Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm developing an windows application for converting images for using in android boot animation. The images for boot animation should be in PNG format and without interlace or transparency.

How can i save System.Drawing.Image object as PNG formatted file without interlace or transparency in VB/C# .NET....?

What I have tried:

I had read this... PngBitmapEncoder Class (System.Windows.Media.Imaging)[^]

But don't know how to use this class with System.Drawing.Image
Posted
Updated 21-Jul-17 11:09am

1 solution

This seems to work.
Save the System.Drawing.Image to a MemoryStream.
Create a PngBitmapEncoder and set the options that you want.
Add a BitmapFrame to the PngBitmapEncoder from that memory stream.
Then save the PNG using the PngBitmapEncoder.

I hope this helps. Sorry I don't know how to control the transparency settings.


C#
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;

namespace plng
{
    class Program
    {
        static void Main(string[] args)
        {
            Image img = Bitmap.FromFile("foo.bmp");

            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Bmp);
                ms.Seek(0, SeekOrigin.Begin);

                PngBitmapEncoder enc = new PngBitmapEncoder();
                enc.Interlace = PngInterlaceOption.Off;
                enc.Frames.Add(BitmapFrame.Create(ms));

                using (FileStream ostream = new FileStream("bar.png", FileMode.Create))
                {
                    enc.Save(ostream);
                }
            }

        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900