Create PNG file programmatically from GIF files?

J

John Williams

Does anyone know a way of creating a PNG file containing several GIF
files programmatically (using Visual Basic)?

Here's what I do manually in Fireworks. Each Gif is 200 x 200 pixels.

1. Create a new canvas, 400 x 400.
2. Import Gif 1 and position in top left corner (x=0, y=0)
3. Import Gif 2 and position in bottom left corner (x=0, y=200)
4. Import Gif 3 and position in top right corner (x=200, y=0)
5. Import Gif 4 and position in bottom right corner (x=200, y=200)
6. Save Png file.

I would like to do the above within a VB program. A COM object or API
to do the above would be very useful. I've searched for PNG encoders
but they all seem to be for Java apps, and don't offer the precise x,y
positioning I require.

Thanks for any help.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (John Williams) scripsit:
Does anyone know a way of creating a PNG file containing several GIF
files programmatically (using Visual Basic)?

Here's what I do manually in Fireworks. Each Gif is 200 x 200 pixels.

1. Create a new canvas, 400 x 400.
2. Import Gif 1 and position in top left corner (x=0, y=0)
3. Import Gif 2 and position in bottom left corner (x=0, y=200)
4. Import Gif 3 and position in top right corner (x=200, y=0)
5. Import Gif 4 and position in bottom right corner (x=200, y=200)
6. Save Png file.

I would like to do the above within a VB program. A COM object or API
to do the above would be very useful. I've searched for PNG encoders
but they all seem to be for Java apps, and don't offer the precise x,y
positioning I require.

Untested:

\\\
Dim b As New Bitmap(200, 200, ...)
Dim g As Graphics = Graphics.FromImage(b)
g.DrawImage(...)
g.DrawImage(...)
g.DrawImage(...)
g.DrawImage(...)
g.Dispose()
b.Save(...)
b.Dispose()
///
 
J

John Williams

Thank you, both. This does indeed look like what I need. I'll investigate further.
 
J

John Williams

Thank you, both. This does indeed look like what I need. I'll investigate further.

The following code almost achieves what I want. It creates a 400 x
400 pixel Png file containing 4 Gifs. Each Gif is 200 x 200.

However the resultant Png contains 1 bitmap, whereas the manual
Fireworks procedure creates 4 bitmaps each containing 1 image which
can be manipulated individually. I would like the program to do the
same. Any ideas?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strGifs(3) As String
Dim i, pos(3, 1) As Integer
Dim oBmpPng As Bitmap
Dim oGraphics As Graphics
Dim oImgTile As Image

'Creates a 400 x 400 pixel Png file containing 4 Gifs. Each
Gif is 200 x 200

strGifs(0) = "TQ09NW04.gif"
strGifs(1) = "TQ09NW08.gif"
strGifs(2) = "TQ09NW24.gif"
strGifs(3) = "TQ09NW28.gif"

pos(0, 0) = 0 : pos(0, 1) = 0
pos(1, 0) = 0 : pos(1, 1) = 200
pos(2, 0) = 200 : pos(2, 1) = 0
pos(3, 0) = 200 : pos(3, 1) = 200

oBmpPng = New Bitmap(400, 400)
oGraphics = Graphics.FromImage(oBmpPng)

For i = 0 To 3
oImgTile = Image.FromFile(strFolder + strGifs(i))
oGraphics.DrawImage(oImgTile, pos(i, 0), pos(i, 1))
Next
oBmpPng.Save(strFolder + "test.png")
oBmpPng.Dispose()

End Sub
 
J

John Williams

The following code almost achieves what I want. It creates a 400 x
400 pixel Png file containing 4 Gifs. Each Gif is 200 x 200.

However the resultant Png contains 1 bitmap, whereas the manual
Fireworks procedure creates 4 bitmaps each containing 1 image which
can be manipulated individually. I would like the program to do the
same. Any ideas?

And I don't think it is anything to do with multiframe images because
the Png file created by Fireworks has a Frame Count of 1 and a Bit
Depth of 32. A Gif file has a Frame count of 1 and a Bit Depth of 8.
 
C

Cor

Hi John,

I never made a png file, but in the link I gave you I see this code,
I see it nowhere in your code ????

// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

CLSID encoderClsid;
Status stat;
Image* image = new Image(L"Bird.bmp");

// Get the CLSID of the PNG encoder.
GetEncoderClsid(L"image/png", &encoderClsid);

stat = image->Save(L"Bird.png", &encoderClsid, NULL);

if(stat == Ok)
printf("Bird.png was saved successfully\n");
else
printf("Failure: stat = %d\n", stat);

delete image;
GdiplusShutdown(gdiplusToken);
return 0;

Cor
 
J

John Williams

Cor said:
Hi John,

I never made a png file, but in the link I gave you I see this code,
I see it nowhere in your code ????
// Get the CLSID of the PNG encoder.
GetEncoderClsid(L"image/png", &encoderClsid);

stat = image->Save(L"Bird.png", &encoderClsid, NULL);

Thanks for your reply, however the example is odd because there is no
Save method for images which uses the Encoder Clsid.

Using 3 of the available Bitmap Save methods, makes no difference to
the output png file. The 3 methods are shown below. Test1.png, 2 and
3 are exactly the same.

Private Sub createPng
Dim strGifs(3) As String
Dim i, pos(3, 1) As Integer
Dim oBmpPng As Bitmap
Dim oGraphics As Graphics
Dim oImgTile As Image
Dim pngFormat As ImageFormat
Dim pngGuid As Guid
Dim pngCodec As ImageCodecInfo
Dim encoderParams As EncoderParameters

pngCodec = GetEncoder("image/png")
pngFormat = ImageFormat.Png

'Creates a 400 x 400 pixel Png file containing 4 Gifs. Each
Gif is 200 x 200

strGifs(0) = "TQ09NW04.gif"
strGifs(1) = "TQ09NW08.gif"
strGifs(2) = "TQ09NW24.gif"
strGifs(3) = "TQ09NW28.gif"

pos(0, 0) = 0 : pos(0, 1) = 0
pos(1, 0) = 0 : pos(1, 1) = 200
pos(2, 0) = 200 : pos(2, 1) = 0
pos(3, 0) = 200 : pos(3, 1) = 200

oBmpPng = New Bitmap(400, 400, PixelFormat.Format32bppRgb)
oGraphics = Graphics.FromImage(oBmpPng)

For i = 0 To 3
oImgTile = Image.FromFile(strFolder + strGifs(i))
oGraphics.DrawImage(oImgTile, pos(i, 0), pos(i, 1))
Next
oBmpPng.Save(strFolder + "test1.png")
oBmpPng.Save(strFolder + "test2.png", pngCodec, encoderParams)
oBmpPng.Save(strFolder + "test3.png", pngFormat)
oBmpPng.Dispose()
oGraphics.Dispose()
oImgTile.Dispose()
End Sub

Private Function GetEncoder(ByVal mimeType As String) As
Imaging.ImageCodecInfo
Dim encoders() As Imaging.ImageCodecInfo =
Imaging.ImageCodecInfo.GetImageEncoders()
Dim i As Integer

For i = 0 To encoders.Length - 1
If encoders(i).MimeType = mimeType Then
Return encoders(i)
End If
Next
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top