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

转载 2012年03月29日 09:57:46

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

相关文章推荐

C#图片压缩处理

最近在做相册功能遇到一个问题,就是加载图片的时候然后放大,感觉有点卡。图片可能有点大,所以考虑用缩略图来实现。 也就是在查询出来一个相册图片  ,一页显示几十张,这几十张用缩略图来显示,看相信情况的...

C# 图片加载、导出、缩放、质量、缩略图

using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; us...

C#处理TIFF格式

using   System; using   System.IO; using   System.Drawing; using   System.Drawing.Imaging;   using  ...

c# GDI+ Image.Save()或者Bitmap.Save()方法保存高质量图片

GDI+保存质量比较高的图片的方法,使用Image或者Bitmap的Save方法调节图像保存的质量。 Image.Save方法共有五种重载  其中Save(String, ImageCod...

一种使用GDI+对图片尺寸和质量的压缩方法

该文介绍了两种图片压缩算法。这两种方法都是通过GDI+技术实现。其中一种是质量压缩:图片尺寸不变,质量降低,大小降低。另一种是尺寸压缩。该文介绍的两种方法对JPEG、bmp和PNG都有效。...

GDI+ 创建缩略图

一师是个好学校! GDI+是个好东西.

A Simple JPEG Encoder in C#

A Simple JPEG Encoder in C# By Arpan Jati | 2 Jun 2010 C#Windows.NETGDI+IntermediateAdvanced ...
  • zanfeng
  • zanfeng
  • 2011年09月24日 13:46
  • 1571

C#对TIF图片格式压缩

               Encoder enc = Encoder.SaveFlag;                EncoderParameters ep = null;          ...

关于Socket和NetWorkstream介绍

 http://hi.baidu.com/xiaopengzyz/blog/item/cd09380e1a3c83e7aa64571d.html我们知道C#和C++的差异之一,就是他本身没有类库,所使...
  • wanglee
  • wanglee
  • 2009年11月25日 17:25
  • 13447

关于EMGU CV的那些事——2.摄像头捕捉(RGB and GRAY)

今天我们更进一步,来看看Emgu CV如何捕捉摄像头并显示视频。 思路很简单,Capture cap = new Capture()来开启一个新的摄像头,cap.Start()开始图像捕捉。利用cap...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C# System.Drawing.Imaging下的 Encoder 的一些属性
举报原因:
原因补充:

(最多只允许输入30个字)