2 Replies - 3361 Views - Last Post: 02 January 2014 - 06:15 AM Rate Topic: -----

#1 kamikazehighland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-January 13

Convert PNG to 32bpp Bitmap

Posted 17 January 2013 - 08:23 PM

I need to create a bitmap object that either preserves the alpha channel of a png or adds one if the png has no alpha channel. This:
Dim a = New Bitmap( _
    My.Application.Info.DirectoryPath & "\a.png")
            Dim b As New Bitmap(a)

Will alter the colors or alpha values. I use the Image.Save method described here to convert pngs to bmps:
Now I just need to convert pngs to 32bppArgb bitmaps instead.

The msdn example saves to a file, but I save to a stream instead. There is only one ImageFormat.Bmp so what do I do?

This is the 'GetEncoderInfo' function from msdn:
Private Shared Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
        Dim j As Integer 
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()

        j = 0
        While j < encoders.Length
            If encoders(j).FormatID = format.Guid Then 
                Return encoders(j)
            End If
            j += 1
        End While 
        Return Nothing 

    End Function

Can I write that to convert to 32bppArgb bitmap instead?

Is This A Good Question/Topic? 0
  • +

Replies To: Convert PNG to 32bpp Bitmap

#2 tlhIn`toq  Icon User is offline

  • Freelance developer
  • member icon

Reputation: 6053
  • View blog
  • Posts: 13,317
  • Joined: 02-June 10

Re: Convert PNG to 32bpp Bitmap

Posted 18 January 2013 - 03:48 PM

You should look at the various overrides for New Bitmap.
You'll see that one of them lets you specify the depth.

This article has a simple example just going the opposite direction from 32 to 24. But you can see how all you have to do it select the 32bpp enumeration.
Was This Post Helpful? 0
  • +
  • -

#3 Al_Magrebee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-January 14

Re: Convert PNG to 32bpp Bitmap

Posted 02 January 2014 - 06:15 AM

I have worked with transparent images and their conversion in my previous application.
For the simple case of converting 32-bit transparent png to 32-bit bmp with alpha, the code below using standard .NET classes works correctly:
Dim a = New Bitmap(My.Application.Info.DirectoryPath & "\Test32bit.png")
a.Save("convertedfile32bit.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

And to save to a stream you can use the stream overload for Image.Save()
Dim ms = New MemoryStream()
a.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)

Note that this code does not cover all PNG cases. For example if the transparent image is 8-bit image, for which PNG supports at least 2 different types of transparency, you will need more advanced techniques and I haven't found the solution for those using standard .NET classes, so I used a commercial imaging SDK for that called Leadtools.

About the other part of your question, if you want to add an Alpha channel to an image that doesn't have one, one way to do this is by accessing the image's pixels one by one and setting the alpha values. The following two links contains some code snippets that I think will be useful to you:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1