Image Format Problems

code Mechanic

Member
Joined
Nov 17, 2003
Messages
8
Location
Cheshire England
Hi Folks,

I have written a custom graph control, I want to be able to allow users to export an image of the control to disk in several formats. The only fomat that looks ok is ".png" files. The others have a black back ground and all the other colours are screwed.

Heres the code:

Public Sub ExportImage(ByVal strFileName As String)

Dim MyImage As Bitmap = New Bitmap(Me.Width, Me.Height)
Dim Gfx As Graphics = Graphics.FromImage(MyImage)

DrawGraph(Gfx)
DrawPoints(Gfx)
DrawCurve(Gfx)

Select Case GetFileExtension(strFileName).ToLower
Case "bmp"
MyImage.Save(strFileName, ImageFormat.Bmp)
Case "jpg"
MyImage.Save(strFileName, ImageFormat.Jpeg)
Case "gif"
MyImage.Save(strFileName, ImageFormat.Gif)
Case "png"
MyImage.Save(strFileName, ImageFormat.Png)
End Select

Gfx.Dispose()

End Sub

Any ideas on how to correct this?

Thanks in advance.

CM
 
That is because, formats like jpeg dont have alpha chanels. So if you want to export it to formats other than png, draw a background for the image first....
 
Image Quality No Problems

wessamzeidan said:
That is because, formats like jpeg dont have alpha chanels. So if you want to export it to formats other than png, draw a background for the image first....

Cheers Wessamzeidan,

I was aware of the alpha chanels, but didnt know about drawing the background first, so many thanks:

Final Code, works great:

Public Sub ExportImage2(ByVal strFileName As String)

Dim MyImage As Bitmap = New Bitmap(Me.Width, Me.Height)
Dim Gfx As Graphics = Graphics.FromImage(MyImage)
Dim IntYCounter As Integer
Dim IntXCounter As Integer

For IntXCounter = 3 To MyImage.Width - 3
For IntYCounter = 3 To MyImage.Height - 3
MyImage.SetPixel(IntXCounter, IntYCounter, Color.White)
Next
Next

DrawGraph(Gfx)
DrawPoints(Gfx)
DrawCurve(Gfx)

Select Case GetFileExtension(strFileName).ToLower
Case "bmp"
MyImage.Save(strFileName, ImageFormat.Bmp)
Case "jpg"
MyImage.Save(strFileName, ImageFormat.Jpeg)
Case "gif"
MyImage.Save(strFileName, ImageFormat.Gif)
Case "png"
MyImage.Save(strFileName, ImageFormat.Png)
End Select

Gfx.Dispose()

End Sub

Thanks Again

CM
 
Back
Top