![]() |
Click here to advertise with us
|
|
|||||||
| Graphics/GDI+ Graphics discussion for VB.NET applications, Winforms, Web, Compact Framework, etc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
If I set CloneFirst =True, the image is displayed as expected. If i Set CloneFirst =False the image is displayed pixelated. Can someone please explain to me why this is happening.
Code:
Private Const FILE_PATH As String = "staticmap.jpeg"
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim imageFile As New IO.FileInfo(FILE_PATH)
Dim newBitMap As Bitmap = CType(Bitmap.FromStream(imageFile.OpenRead), Bitmap)
Dim clonedBitMap As Bitmap
clonedBitMap = getBitMap(newBitMap, ckbCloneFirst.Checked)
pbImage.Image = clonedBitMap
End Sub
Private Function getBitMap(ByVal BottomImage As Bitmap, ByVal CloneFirst As Boolean) As Bitmap
Dim CopiedBottomImage As Bitmap = DirectCast(BottomImage.Clone(), Bitmap)
Dim CopiedImageGraphics As Graphics
Dim otherImage As Bitmap
If CloneFirst Then
otherImage = DirectCast(BottomImage.Clone(), Bitmap)
CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
Else
CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
otherImage = DirectCast(BottomImage.Clone(), Bitmap)
End If
'otherImage.Dispose()
'BottomImage.Dispose()
'' secondImage.Dispose()
'CopiedImageGraphics.Dispose()
Return CopiedBottomImage
End Function
|
|
||||
|
Quote:
Quote:
You have commented out some Dispose calls here, Graphics instances created from FromImage must be disposed when done using them, image/bitmap objects must also be disposed after use. So otherImage.Dispose() and CopiedImageGraphics.Dispose() is needed in getBitMap method. In btnDisplay_Click method you need to dispose the image object held by newBitMap variable, and if pbImage.Image already contains an image you must dispose that before assigning it a new image object. imageFile.OpenRead returns a FileStream object, you have to close/dispose this when done using it also. In general you have to analyse your code to make sure you don't open/create objects and just leave them in memory after last reference is released without disposing/closing them properly.
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
yes, I know you have to dispose of the image after the use, I was commenting out the code trying to figure out what was going on.
So I tried Saving the Image to demonstrate what I am seeing, however, when i save the image, the image does not display incorrectly any more. Code:
Private Function getBitMap(ByVal BottomImage As Bitmap, ByVal CloneFirst As Boolean) As Bitmap
Dim CopiedBottomImage As Bitmap = DirectCast(BottomImage.Clone(), Bitmap)
Dim CopiedImageGraphics As Graphics
Dim otherImage As Bitmap
If CloneFirst Then
otherImage = DirectCast(BottomImage.Clone(), Bitmap)
CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
Else
CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
otherImage = DirectCast(BottomImage.Clone(), Bitmap)
End If
otherImage.Dispose()
CopiedImageGraphics.Dispose()
'Fixes the Problem
CopiedBottomImage.Save(SAVE_IMAGE_PATH)
Return CopiedBottomImage
End Function
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|