When I create a thumbnail of an existing transparent png file the new background is black.
How do I make it transparent?
Dim strPath As String
Dim Width As Integer = 110
Dim Height As Integer = 55
strPath = savePath
Dim oldImage As Image = System.Drawing.Image.FromFile(strPath)
Dim thumbNail = New Bitmap(110, 55, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim oGraphic As Graphics = Graphics.FromImage(thumbNail)
'Does not work
'oGraphic.Clear(Color.Transparent)
oGraphic.CompositingQuality = CompositingQuality.HighSpeed
oGraphic.SmoothingMode = SmoothingMode.HighSpeed
oGraphic.InterpolationMode = InterpolationMode.Low
Dim rect As New Rectangle(0, 0, Width, Height)
oGraphic.DrawImage(oldImage, rect)
If InStr(savePath, ".png", CompareMethod.Text) > 0 Then
strPath = Replace(strPath, ".png", "Thumbnail.png")
thumbNail.Save(strPath, ImageFormat.Png)
End If
It seems that the following changes make it work for png's
'Change - Dim thumbNail = New Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim thumbNail = New Bitmap(destWidth, destHeight)
and
After
Dim rect As New Rectangle(0, 0, destWidth, destHeight)
oGraphic.DrawImage(oldImage, rect)
Add
Dim ms As New System.IO.MemoryStream()
thumbNail.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
This works to and resizes the image proportionally
Dim strPath As String
Dim Width As Integer = 110
Dim Height As Integer = 55
strPath = psavePath
Dim oldImage As Image = System.Drawing.Image.FromFile(strPath)
Dim sourceWidth As Integer = oldImage.Width 'intOldWidth
Dim sourceHeight As Integer = oldImage.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim nPercent As Single = 0
Dim nPercentW As Single = 0
Dim nPercentH As Single = 0
nPercentW = (CSng(Width) / CSng(sourceWidth))
nPercentH = (CSng(Height) / CSng(sourceHeight))
If nPercentH < nPercentW Then
nPercent = nPercentH
destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2)
Else
nPercent = nPercentW
destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2)
End If
Dim destWidth As Integer = CInt(Math.Truncate(sourceWidth * nPercent))
Dim destHeight As Integer = CInt(Math.Truncate(sourceHeight * nPercent))
'Dim thumbNail = New Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim thumbNail = New Bitmap(destWidth, destHeight)
Dim oGraphic As Graphics = Graphics.FromImage(thumbNail)
If InStr(psavePath, ".jpg", CompareMethod.Text) > 0 Then
oGraphic.Clear(Color.White)
End If
oGraphic.CompositingQuality = CompositingQuality.HighSpeed
oGraphic.SmoothingMode = SmoothingMode.HighSpeed
oGraphic.InterpolationMode = InterpolationMode.Low
Dim rect As New Rectangle(0, 0, destWidth, destHeight)
oGraphic.DrawImage(oldImage, rect)
'Change file name for thumbnail
If InStr(psavePath, ".jpg", CompareMethod.Text) > 0 Then
strPath = Replace(strPath, ".jpg", "Thumbnail.jpg")
thumbNail.Save(strPath, ImageFormat.Jpeg)
End If
If InStr(psavePath, ".png", CompareMethod.Text) > 0 Then
strPath = Replace(strPath, ".png", "Thumbnail.png")
thumbNail.Save(strPath, ImageFormat.Png)
End If
The PixelFormat Format24bppRgb doesn't have an alpha channel for transparency. Try
Format32bppArgb instead as it provides 8 bits each for RGB and an alpha channel.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
Member
29 Points
966 Posts
Background of the newly created png file is black
Oct 09, 2013 12:42 PM|craigbtx|LINK
When I create a thumbnail of an existing transparent png file the new background is black.
How do I make it transparent?
Member
29 Points
966 Posts
Re: Background of the newly created png file is black
Oct 09, 2013 04:52 PM|craigbtx|LINK
It seems that the following changes make it work for png's
'Change - Dim thumbNail = New Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim thumbNail = New Bitmap(destWidth, destHeight)
and
After
Dim rect As New Rectangle(0, 0, destWidth, destHeight)
oGraphic.DrawImage(oldImage, rect)
Add
Dim ms As New System.IO.MemoryStream()
thumbNail.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Dont get it but
Member
29 Points
966 Posts
Re: Background of the newly created png file is black
Oct 09, 2013 07:22 PM|craigbtx|LINK
This works to and resizes the image proportionally
All-Star
26071 Points
5892 Posts
Re: Background of the newly created png file is black
Nov 03, 2013 01:48 AM|markfitzme|LINK
The PixelFormat Format24bppRgb doesn't have an alpha channel for transparency. Try Format32bppArgb instead as it provides 8 bits each for RGB and an alpha channel.