在 C# 中处理图像。调整 Gif 大小并将其另存为 JPEG

发布于 2024-10-30 03:07:11 字数 3289 浏览 3 评论 0原文

您好,我使用 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();
    }

Hi I use C# ans Asp.Net4.

I create a class which re size a source image.
Code work great with JPEG File but if I upload a GIF I receive this error:

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

Here my code.. Any Ideas?

Exception line: 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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫言歌 2024-11-06 03:07:11

我认为这就是问题所在:

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

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

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

I think this is the problem:

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

You are using the pixel format of the image you have just read in - in this case GIF, so this is what is failing.

You need to use a non-index pixel format for this.

萌面超妹 2024-11-06 03:07:11

只需将这一行更改

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

为:

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

Just change this line:

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

into

SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文