关闭

C# System.Drawing.Imaging下的 Encoder 的一些属性

标签: c#objectshapescompressiontiffstring
6399人阅读 评论(0) 收藏 举报
分类:

Quality:

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

ColorDepth:

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:

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图片来说,都木有反应。很悲剧!


代码如下:

			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,那你就牛了。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论