locked
Background of the newly created png file is black RRS feed

  • Question

  • User-430413477 posted

    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

    Wednesday, October 9, 2013 12:42 PM

All replies

  • User-430413477 posted

    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

    Wednesday, October 9, 2013 4:52 PM
  • User-430413477 posted

    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

    Wednesday, October 9, 2013 7:22 PM
  • User465171450 posted

    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.

    Sunday, November 3, 2013 1:48 AM