将system.drawing.image保存为PNG,无需.NET中的交错和透明 [英] Save system.drawing.image as PNG without interlace and transparency in .NET

查看:113
本文介绍了将system.drawing.image保存为PNG,无需.NET中的交错和透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于转换图像的Windows应用程序,以便在android启动动画中使用。启动动画的图像应该是PNG格式,没有隔行扫描或透明度。



如何将System.Drawing.Image对象保存为PNG格式文件,无需交错或透明在VB / C#.NET ....?



我尝试过:



我读过这个...... PngBitmapEncoder类(System.Windows.Media.Imaging) [ ^ ]



但不知道如何将此类与System.Drawing.Image一起使用

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

推荐答案

这似乎有效。

将System.Drawing.Image保存到MemoryStream。

创建一个PngBitmapEncoder并设置所需的选项。

从该内存流向PngBitmapEncoder添加一个BitmapFrame。

然后使用PngBitmapEncoder保存PNG。



我希望这会有所帮助。抱歉,我不知道如何控制透明度设置。





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.


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);
                }
            }

        }
    }
}


这篇关于将system.drawing.image保存为PNG,无需.NET中的交错和透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆