Best way to display a JPG (Full Version)

All Forums >> [.NET Compact Framework Development] >> .NET Compact Framework - VB.NET



Message


ymahmood -> Best way to display a JPG (4/22/2008 5:35:41 AM)

Hi,
I have images (jpgs) on the handheld, they are actually photo's. I am trying to show them in a picturebox. I have tried two different ways but both ways i am getting errors related to memory.

The first method i tried was to to simply load the jpg as a bitmap and place into picturebox control ie

pcbPhoto.Image = New Bitmap(pPhotoPath & lFieldVal)


Doing it this way started to give me outofmemoryexception errors after displaying different images a number of times, i think i have understood why after reading up in this issue.

So then I changed code to display the images using the OpennetCf way.

What i did was create a function which returned a bitmap object which i then passed to the image property of the Picturebox control. here is that function:


Function Bitmap_Return(ByVal lPath As String, ByVal lWidth As Integer, ByVal lHeight As Integer) As System.Drawing.Bitmap
Dim lFactory As ImagingFactory = New ImagingFactoryClass()
Dim lImg As IImage
Dim lImgB As IBitmapImage
Dim lErrPos As String = ""

 

Try

 

lFactory.CreateImageFromFile(lPath, lImg)
 

lFactory.CreateBitmapFromImage(lImg, lWidth, lHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb, InterpolationHint.InterpolationHintDefault, lImgB)
 

Bitmap_Return = ImageUtils.IBitmapImageToBitmap(lImgB)



System.Runtime.InteropServices.Marshal.ReleaseComObject(lFactory)

System.Runtime.InteropServices.Marshal.ReleaseComObject(lImg)


System.Runtime.InteropServices.Marshal.ReleaseComObject(lImgB)
 

GC.Collect()
 

Catch ex As Exception
MsgBox(
" Err: " & ex.Message, MsgBoxStyle.Critical, "Bitmap_Return")
 

End Try
End Function
 
 
I had to add the garbage collection because the file that i was opening was being locked by the OpennetCF, and GC released it.
 
Any how, i am now getting another error, again the error does not happen all the time: "Not enough storage is available to complete this operation". When I get the error I check the memory status through settings and it seems fine. I cannot work out why apart from possibly a bug on OpennetCF side of things. I have rasied it with them but mean time need need a third way of displaying a photo in a picture box control, Any ideas ??
 
Thanks in advance.
 




petevick -> RE: Best way to display a JPG (4/22/2008 12:48:02 PM)

Hi,
when you did it the first way, did you dispose of the object, before loading the next picture - I guess that is what is consuming the memory.





ymahmood -> RE: Best way to display a JPG (4/24/2008 4:44:31 AM)

Pete,
You mentioned "dispose of the object" I assume you are talking about the picturebox.
The pcbPhoto is a picturebox that i am creating dynamically and adding it to a panel. Before i add any controls to that panel I do a Panel.controls.clear. From what i understand doing a clear should be handling the dispose of each object including the picturebox.
Am i right in assuming that or do i need to get rid the of the controls a different way?

Thanks




petevick -> RE: Best way to display a JPG (4/24/2008 9:19:49 AM)

Hi,
try doing

Try
pcbphoto.image.dispose
catch
end try

pcbphoto.image = new bitmap....

Pete




ymahmood -> RE: Best way to display a JPG (4/24/2008 10:18:02 AM)

Pete,

Do i still need to do that even though i am creating the picturebox on the fly.
From what you are saying i should do the following

Panel1.controls.clear
dim lPCB as new PictureBox
lPCB.Image.Dispose
lPCB.image = new Bitmap (.....
Panel1.Controls.Add(lPCB)


Is that what you mean?




petevick -> RE: Best way to display a JPG (4/24/2008 12:03:53 PM)

Do you create it on the fly every time you load a picture?

If so, you should dispose of the control when you have finished with it.




ymahmood -> RE: Best way to display a JPG (4/25/2008 10:26:13 AM)

Pete,
I think i have located what this message "Not enough Storage is available ..." means.

I think i am running out of Virtual Memory. Basically i am logging the Program memory and virtual memory using the following call
GlobalMemoryStatus and the virtual memory is dropping even though i am doing garbage collections etc etc.

I start to get above errors when virtual memory drops from 19202048 down to about 2621440 which i think means approx 2.5 mb.
 

Now that i have a better idea on whats going on, what are my options inorder to resolve this issue of running out of VM.

Thanks so far.




ymahmood -> RE: Best way to display a JPG (4/25/2008 11:22:24 AM)

Has picturebox control got a memory leak.

Bypassing the picturebox control and not creating one, the trace showed that i had gained VM from 19136512  to 20054016.
But when i uncomment the picture box code i am losing Virtual memory.

It seems to me that every time I create a Picturebox on the fly it uses up VM and does not release it again.




petevick -> RE: Best way to display a JPG (4/25/2008 11:29:53 AM)

Hi,
why are you creating it on the fly - why not just use one?

As I said before, if you are creating, and not disposing, you will eat up memory




ymahmood -> RE: Best way to display a JPG (4/29/2008 5:01:55 AM)

Pete,

Basically the controls are generated on the fly because whether they exist or not is based upon data.

I am disposing of the image and all other controls on the form by doing the following:

Dim lY As Integer

For lY = 0 To pnlForm.Controls.Count - 1

  Try

  If pnlForm.Controls(lY).GetType.Name = "PictureBox" Then

    Dim lbm As PictureBox = pnlForm.Controls(lY)
  lbm.Image.Dispose()
  lbm.Dispose()
  pnlForm.Controls(lY).Dispose()
  Else

  pnlForm.Controls(lY).Dispose()
  End If

  Catch ex As Exception
  End Try

Next

pnlForm.Controls.Clear()



Is that the best way?
Thanks pete!




petevick -> RE: Best way to display a JPG (4/30/2008 1:54:39 AM)

Hi,
looks ok to me :)

Do you still get memory problems?

Pete




ymahmood -> RE: Best way to display a JPG (5/1/2008 4:23:30 AM)

Pete,

Yes, It's not recovering the Virtual memory.

What else can i try?




petevick -> RE: Best way to display a JPG (5/1/2008 9:12:59 AM)

Force garbage collection?




ymahmood -> RE: Best way to display a JPG (5/1/2008 11:19:47 AM)

Pete,
I am , after the controls.clear I am doing a gc.collect

I think i have come to the end of this... gonna have to do it another way.

thanks for the help.




petevick -> RE: Best way to display a JPG (5/1/2008 1:52:37 PM)

HI,
after loading howmany pictures do you get the error?

Pete




ymahmood -> RE: Best way to display a JPG (5/2/2008 5:26:11 AM)

Pete,

Basically I am going in and out of a screen, that screen dynamically creates different controls on the fly, one of them is a picturebox which also loads an image using opennetCF method. The image is approx 17kb. I'll get the error after approx 40 mins of going in and out of that screen. The memory trace shows me that when i get the error is when I run out of Virtual memory.

Just another point which may or may not be relevant. I am not using the actual picturebox but my own class which is based on the picturebox. here is the class:


Public
Class PhotoControl
Inherits System.Windows.Forms.PictureBox
 
Dim lMandatory As Boolean

Dim lFileName As String

Dim lText As String




Public Property Mandatory() As Boolean

Get

Return lMandatory
End Get

Set(ByVal value As Boolean)
lMandatory = value
End Set

End Property
 

Public Property FileName() As String

Get

Return lFileName
End Get

Set(ByVal value As String)
lFileName = value
End Set

End Property
 

Public Property Text() As String

Get

Return lText
End Get

Set(ByVal value As String)
lText = value
End Set

End Property
 


End Class



This allows me to add some more properties to the control.

thanks!




Page: [1]



Forum Software © ASPPlayground.NET Advanced Edition 2.5.5 Unicode

0.016