【问题标题】:Working with Images in C#. Resize a Gif and save it as JPEG在 C# 中处理图像。调整 Gif 大小并将其保存为 JPEG
【发布时间】:2011-07-29 00:51:56
【问题描述】:

您好,我使用 C# 和 Asp.Net4。

我创建了一个重新调整源图像大小的类。 代码适用于 JPEG 文件,但如果我上传 GIF,我会收到此错误:

A Graphics object cannot be created from an image that has an indexed pixel format.

这是我的代码.. 有什么想法吗?

异常行:SD.Graphics newImage = SD.Graphics.FromImage(temp);

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


    protected void ResizeImageWithAspect(string fileName, string outputFileName, int newWidth, int newResolution, string newCodec, int qualityLevel)
    {
        // Original Image
        SD.Image original = SD.Image.FromFile(fileName);

        // Find image aspect ratio computed by image height and width.
        float aspect = (float)original.Height / (float)original.Width;

        // Calculate the new height using the aspect ratio and the desired new width.
        int newHeight = (int)(newWidth * aspect);

        // Create a bitmap of the correct size. Bitmap it is used for image manipolation.
        SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat);

        // Setting the Encoder for the image.
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder; // Guid Encoder
        EncoderParameter myEncoderParameter; // Pass a value to an Image Encoder
        EncoderParameters myEncoderParameters; //Encapsulates an array of EncoderParameter objects

        // Get an ImageCodecInfo object that represents the JPEG codec.
        myImageCodecInfo = GetEncoderInfo(newCodec);
        // 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);
        myEncoderParameter = new EncoderParameter(myEncoder, qualityLevel);
        myEncoderParameters.Param[0] = myEncoderParameter;

        //Set image resolution (horizontal and vertical)
        temp.SetResolution(newResolution, newResolution);

        //Get a Graphics object from the bitmap.
        SD.Graphics newImage = SD.Graphics.FromImage(temp);
        // Quality settings output image
        newImage.SmoothingMode = SmoothingMode.AntiAlias;
        newImage.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        newImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
        newImage.PixelOffsetMode = PixelOffsetMode.HighQuality;

        //Draw the image with the new width/height
        newImage.DrawImage(original, 0, 0, newWidth, newHeight);

        //Save the bitmap with appropirate Codec
        temp.Save(outputFileName, myImageCodecInfo, myEncoderParameters);

        //Dispose of our objects.
        original.Dispose();
        temp.Dispose();
        newImage.Dispose();
    }

【问题讨论】:

  • 哪一行给出了错误?由于 GIF 图像使用索引来指定每个像素的颜色而不是显式的 RGB 值,因此您需要使用不同的方法来读取它。
  • SD.Graphics newImage = SD.Graphics.FromImage(temp);
  • 上面的行创建了错误。你能给我一个不同方法的例子吗? PNG文件也有同样的问题吗?
  • 我快速浏览了一下 - 但它应该可以工作。我原以为SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat); 行会失败,因为您正在使用将被索引的图像像素格式。

标签: c# asp.net image-manipulation gdi


【解决方案1】:

我认为这是问题所在:

   SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat);

您正在使用刚刚读入的图像的像素格式 - 在本例中为 GIF,所以这就是失败的原因。

您需要为此使用非索引像素格式。

【讨论】:

    【解决方案2】:

    只需更改此行:

    SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat); 
    

    进入

    SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2011-01-25
      • 1970-01-01
      • 2011-11-17
      • 2011-10-24
      • 2013-11-20
      • 2013-08-20
      相关资源
      最近更新 更多