2

我有如下功能:

    // Convert To JPG 
    // 
    public string AlltoJPG(FileInfo foo)
    {
        // Get file extension
        string fileExtension = foo.Extension;

        // Get file name without extenstion
        string fileName = foo.Name.Replace(foo.Extension, string.Empty) + ".jpg";

        /*------------------------------------------------------------------------*/

        /// <Check for PNG File Format>
        if (fileExtension == ".png" || fileExtension == ".PNG")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Assumes img is the PNG you are converting
            using (Bitmap b = new Bitmap(img.Width, img.Height))
            {
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b))
                {
                    g.Clear(System.Drawing.Color.White);
                    g.DrawImageUnscaled(img, 0, 0);
                }

                // Save the image as a JPG
                b.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

        }

        /*------------------------------------------------------------------------*/

        /// <Check for GIF File Format>
        if (fileExtension == ".gif" || fileExtension == ".GIF")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Construct a bitmap from the image resource.
            Bitmap bmp1 = new Bitmap(img.Width, img.Height);

            // Save the image as a JPG
            bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /*------------------------------------------------------------------------*/

        /// <Check for BMP File Format>
        if (fileExtension == ".bmp" || fileExtension == ".BMP")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Construct a bitmap from the image resource.
            Bitmap bmp1 = new Bitmap(img.Width, img.Height);

            // Save the image as a JPG
            bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /*------------------------------------------------------------------------*/

        /// <Check for TIFF File Format>
        if (fileExtension == ".tiff" || fileExtension == ".TIFF")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Construct a bitmap from the image resource.
            Bitmap bmp1 = new Bitmap(img.Width, img.Height);

            // Save the image as a JPG
            bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /*------------------------------------------------------------------------*/
        fileName = foo.DirectoryName + "\\" + fileName;
        return fileName;
    }

我正在尝试将bmp, png, gif,tiff文件格式转换为jpg, 但GDI+如下所示:

System.OutOfMemoryException 未处理
Message=Bellek Yetersiz。
Source=System.Drawing
StackTrace:
konum: System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
konum: System.Drawing.Image.FromFile(String filename)

PNG那么我怎样才能避免这种情况并至少将BMP文件转换为JPG

这是错误的图片:

在此处输入图像描述

4

2 回答 2

3

我发现了问题!它是由 PNG alpha 通道引起的。

感谢max,下面的链接解决了我的问题:

如何将透明 PNG 加载到位图并忽略 alpha 通道


public static void SetAlpha(this Bitmap bmp, byte alpha)
{
    if(bmp == null) throw new ArgumentNullException("bmp");

    var data = bmp.LockBits(
        new Rectangle(0, 0, bmp.Width, bmp.Height),
        System.Drawing.Imaging.ImageLockMode.ReadWrite,
        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    var line = data.Scan0;
    var eof = line + data.Height * data.Stride;
    while(line != eof)
    {
        var pixelAlpha = line + 3;
        var eol = pixelAlpha + data.Width * 4;
        while(pixelAlpha != eol)
        {
            System.Runtime.InteropServices.Marshal.WriteByte(
                pixelAlpha, alpha);
            pixelAlpha += 4;
        }
        line += data.Stride;
    }
    bmp.UnlockBits(data);
}

用法:

var pngImage = new Bitmap("filename.png");
pngImage.SetAlpha(255);

设置 alpha 后,它开始正常工作。

于 2012-06-15T06:58:35.890 回答
0

gif、png 和 tiff 代码块缺少using子句并且正在泄漏 GDI 引用。

于 2012-06-11T14:02:01.577 回答