Tearing my hair out: drawing transparent image
-
I'm writing an app that opens an image file, converts it to a gif, sets it transparency and then resizes it. Everything works fine until I resize the image (The transparency is lost and is replaced with black pixels). The reason is that to resize the image I am doing a matrix transform and drawing the image onto a new image. When it draws onto the new image it loses its transparency information (I think this is because the image I am getting does not have the right colour depth). I could apply the transparency after resizing but this leaves 'fuzzy edges'. Does anybody have any ideas how I could achieve drawing an image with transparent areas onto a new image. If anyone can help that would be great, I could mail the entire code if necessary, would have posted it here but its too long. Here is a small section the code:
Private Sub ResizeImage(ByVal filename As String, ByVal Width As Integer, ByVal height As Integer) Dim oSourceImage As Bitmap Dim tmpBmp As Bitmap = CType(Image.FromFile(filename), Bitmap) Dim IsAlpha As Boolean = Image.IsAlphaPixelFormat(tmpBmp.PixelFormat) If IsAlpha = False Then oSourceImage = New Bitmap(tmpBmp.Width, tmpBmp.Height, PixelFormat.Format32bppArgb) Dim g As Graphics = Graphics.FromImage(oSourceImage) g.DrawImage(tmpBmp, 0, 0) g.Dispose() tmpBmp.Dispose() Else oSourceImage = tmpBmp End If 'Dim oSourceImage As Image = TransImageUtility.SetTransparentColour(Image.FromFile(filename)) If Not ((oSourceImage.Width <= Width) And (oSourceImage.Height <= height)) Then Dim oResizedImage As New Bitmap(Width, height, PixelFormat.Format32bppArgb) Dim oGraphic As Graphics = Graphics.FromImage(oResizedImage) oGraphic.CompositingQuality = CompositingQuality.HighQuality oGraphic.SmoothingMode = SmoothingMode.HighQuality oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic Dim oRectangle As New Rectangle(0, 0, Width, height) '******************************** Dim largestRatio As Double = Math.Max(CDbl(oSourceImage.Width) / Width, CDbl(oSourceImage.Height) / height) Dim posX As Single = CSng(Width * largestRatio / 2 - oSourceImage.Width / 2) Dim posY As Single = CSng(height * largestRatio / 2 - oSourceImage.Height / 2) Dim mx As New Matrix(1.0F / CSng(largestRatio), 0, 0, 1.0F / CSng(larg