请选择 进入手机版 | 继续访问电脑版

无忧编程_ASP.NET  / C# / PHP 程序员的软件世界

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 618|回复: 0

C# System.Drawing.Imaging下的 Encoder 的一些属性以及保存高质量图片的方法

[复制链接]

349

主题

379

帖子

1565

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1565
发表于 2018-3-20 14:53:25 | 显示全部楼层 |阅读模式
C# System.Drawing.Imaging下的 Encoder 的一些属性以及保存高质量图片的方法
质量相关的类;
[C#] 纯文本查看 复制代码
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetJPEGQuality
{
    public static void Main()
    {
        Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;

        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap("Shapes.bmp");

        // Get an ImageCodecInfo object that represents the JPEG codec.
        myImageCodecInfo = GetEncoderInfo("image/jpeg");

        // Create an Encoder object based on the GUID

        // for the Quality parameter category.
        myEncoder = Encoder.Quality;

        // Create an EncoderParameters object.

        // An EncoderParameters object has an array of EncoderParameter

        // objects. In this case, there is only one

        // EncoderParameter object in the array.
        myEncoderParameters = new EncoderParameters(1);

        // Save the bitmap as a JPEG file with quality level 25.
        myEncoderParameter = new EncoderParameter(myEncoder, 25L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);

        // Save the bitmap as a JPEG file with quality level 50.
        myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);

        // Save the bitmap as a JPEG file with quality level 75.
        myEncoderParameter = new EncoderParameter(myEncoder, 75L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
    }
    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for(j = 0; j < encoders.Length; ++j)
        {
            if(encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
}


颜色深度:
[C#] 纯文本查看 复制代码
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetColorDepth
{
    public static void Main()
    {
        Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;

        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Music\music.bmp");

        // Get an ImageCodecInfo object that represents the TIFF codec.
        myImageCodecInfo = GetEncoderInfo("image/tiff");

        // Create an Encoder object based on the GUID
        // for the ColorDepth parameter category.
        myEncoder = Encoder.ColorDepth;

        // Create an EncoderParameters object.
        // An EncoderParameters object has an array of EncoderParameter
        // objects. In this case, there is only one
        // EncoderParameter object in the array.
        myEncoderParameters = new EncoderParameters(1);

        // Save the image with a color depth of 24 bits per pixel.
        myEncoderParameter =
            new EncoderParameter(myEncoder, 24L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes24bpp.tiff", myImageCodecInfo, myEncoderParameters);
    }

    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for(j = 0; j < encoders.Length; ++j)
        {
            if(encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
}

Compression压缩:
[C#] 纯文本查看 复制代码
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetTIFFCompression
{
    public static void Main()
    {
        Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;

        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap("Shapes.bmp");

        // Get an ImageCodecInfo object that represents the TIFF codec.
        myImageCodecInfo = GetEncoderInfo("image/tiff");

        // Create an Encoder object based on the GUID
        // for the Compression parameter category.
        myEncoder = Encoder.Compression;

        // Create an EncoderParameters object.
        // An EncoderParameters object has an array of EncoderParameter
        // objects. In this case, there is only one
        // EncoderParameter object in the array.
        myEncoderParameters = new EncoderParameters(1);

        // Save the bitmap as a TIFF file with LZW compression.
        myEncoderParameter = new EncoderParameter(
            myEncoder,
            (long)EncoderValue.CompressionLZW);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("ShapesLZW.tif", myImageCodecInfo, myEncoderParameters);
    }
    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for(j = 0; j < encoders.Length; ++j)
        {
            if(encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
}

不过经过自己的一系列测试,本来做一些对图片的裁剪,导出时、设置质量、对文件大小压缩的。

谁不知道,原来C#.net封装的System.Drawing.Imaging下的图形处理类不是支持的。

即使我上面已写好有两个参数可以设置,但对.png图片来说,都木有反应。很悲剧!


代码如下:

[C#] 纯文本查看 复制代码
			S_MyImageCodecInfo = S_GetEncoderInfo("image/png");//ImageCodecInfo.GetImageEncoders()[0];//
			S_MyEncodeParameters = new EncoderParameters(3);
			EncoderParameter myEncoderParameter1 = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L);
			EncoderParameter myEncoderParameter2 = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 32L);//想设置为8位色深,完全无反应,NND
			EncoderParameter myEncoderParameter3 = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);//压缩参数设置为0,还是不行。
			S_MyEncodeParameters.Param[0] = myEncoderParameter1;
			S_MyEncodeParameters.Param[1] = myEncoderParameter2;
			S_MyEncodeParameters.Param[2] = myEncoderParameter3;

			_bitmap.Save(_exportFileName, S_MyImageCodecInfo, S_MyEncodeParameters);

从这看,C#.net并不支持导出时、设置质量、对文件大小压缩的。

如果要非要自己写,那要多大能耐啊。。。汗。


另外,我还自己测试过,我把PhotoShop自己随机编辑一个.png文件(带alpha,有些像素位置没有填颜色)。

文件大小是:519kb

用C#Bitmap读取后,字节数:780.8799kb,导出后,发现字节数为:822.3057kb

变大了,悲剧吧!?


http://topic.csdn.net/u/20120311/18/83e0a879-cf2d-4a89-8b17-5008131270a7.htmlCSDN贴子:这里是我之前发问的。


然后我再试过,把该导出后822.3057kb大小的文件:使用PhotoShop来打开,在导出.png,发现,文件大小为:590kb,虽然比原来的519kb大了。

但可以说明PhotoShop是可以压缩的,处理了一些压缩处理,C#目前的内置类库是不支持的。除非你有这么大能耐,和Adobe 的PhotoShop写出一样可以压缩.png,那你就牛了。

文章转自:http://blog.csdn.net/linjf520/article/details/7405844


windows .net(C#+MSSQL) linux(php+mysql)  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|软件编程 ( 鄂ICP备11006601号鄂公网安备 42011102001337号 |

GMT+8, 2018-7-23 07:46 , Processed in 0.106823 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表