Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

I have a quick question, I have a section of code that watches a directory and creates new thumbnails when a new picture is placed in the directory. However, I cannot do this because I get the error:

 

Controls created on one thread cannot be parented to a control on a different thread.

 

 

I setup an invoke method. However, when I created the delegates and so forth I got the same error. Like I was invoking a method that still was not on the same thread.

 

The section of code is as follows:

 

Public Sub AddThumbnail(ByVal strPath As String)

Dim intRnd As Double = 1

Dim imgImage As Image = Image.FromFile(strPath)

Dim File As FileInfo

Dim Thumbnail As Image = imgImage.GetThumbnailImage(50, 50, Nothing, Nothing)

'Thumbnail.Save(strLocation & "job" & "-" & intRnd & ".jpg")

ImageList1.Images.Add(Image.FromFile(strPath))

Dim pic As New PictureBox()

pic.Image = Thumbnail

pic.Dock = DockStyle.Top

Panel2.Controls.Add(pic)

End Sub

 

Thanks for the help. I am not sure which way to go with this. I need to update Panel2. Again, Thanks for the help.

 

ocbeta

  • *Gurus*
Posted

That method would have to be called from the UI thread of your application. You can call any method like this by defining a delegate with the same signature, and using an existing control's Invoke method to call it.

 

Having said that, the FileSystemWatcher class makes things slightly easier. If you set its SynchronizingObject property to your form when you create it, it will raise its events on the correct thread to begin with and hopefully circumvent your problem.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Thanks for the reply. I was actually able to go about it the way that you suggested. Using the delegate, and MethodInvoker.

 

 

BTW, how would I setup the SynchronizingObject property?

 

Thanks for the Info.

 

I used a simple method invoker to update the control. It seems to work pretty well. I am still hunting down a JIT Generic GDI+ error. But Time will tell.

 

   Private Sub AddThumbnail()  
       Dim File As FileInfo  
       For Each File In Searcher.Matches  
       Next  
       Dim intRnd As Double = 1  
       Dim imgImage As Image = Image.FromFile(strPath)  
       Dim Thumbnail As Image = imgImage.GetThumbnailImage _  
              (50, 50, Nothing, Nothing)  
       ImageList1.Images.Add(Image.FromFile(strPath))  
       Dim pic As New PictureBox()  
       pic.Image = Thumbnail  
       pic.Dock = DockStyle.Top  
       Panel2.Controls.Add(pic)  
   End Sub

 

I know all of this stuff seems kina easy, however, I found the amount of help available pretty lacking in the GDI+ arena. Hopefully this helps some people out.

 

Then :

 

Public Sub Watch_Created(ByVal sender As Object, _  
   ByVal e As System.IO.FileSystemEventArgs)  
       Dim File As File  
       strPath = e.FullPath  
       Dim mi As New MethodInvoker(AddressOf Me.AddThumbnail)  
       Me.BeginInvoke(mi)  
End Sub

 

Thanks,

OCBeta

Posted

Now, I am getting an out of memory error. I even tried gc.collect and that did not seem to help.

 

Here is the error message.

 

System.OutOfMemoryException: Out of memory.

at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)

at System.Drawing.Image.FromFile(String filename)

at ScanPro.Form1.AddThumbnail() in C:\Documents and Settings\Administrator\My Documents\My Visual Studio Projects\Test\Form1.vb:line 267

at System.Windows.Forms.Control.InvokeMarshaledCallbacks()

 

The line in reference is

 

Dim imgImage As Image = Image.FromFile(strPath)

 

 

Thanks for the help guys.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...