bytearray - 创建缩略图,然后转换为字节数组

标签 bytearray thumbnails system.drawing

我有很多时间创建缩略图,然后将它们转换为字节数组。我尝试了三种方法,所有 3 次都出现错误。

第一个是使用Bitmap.GetThumbnailImage,我过去用过,然后直接保存到Response.OutputStream

第二个是使用 System.Drawing.Graphics 和 DrawImage()。还是不行。

第三个就是创建一个新的位图,传入旧的位图,并设置新的大小。同样的错误。

Value cannot be null.
Parameter name: encoder

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: encoder

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:
[ArgumentNullException: Value cannot be null.
Parameter name: encoder]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +615244



这是我的方法的代码。也许有人会看到我做错了什么。如果您不确定 GetThumbSize,它只是一种接收图像大小和最大拇指大小,然后计算实际大小以保持纵横比的方法。
public static System.Drawing.Image.GetThumbnailImageAbort thumbnailCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

    public static bool ThumbnailCallback()
    {
        return false;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="image"></param>
    /// <param name="size"></param>
    /// <remarks>
    /// This method will throw a AccessViolationException when the machine OS running the server code is windows 7.
    /// </remarks>
    /// <returns></returns>
    public static byte[] CreateThumbnail(byte[] imageData, Size size)
    {
        using (MemoryStream inStream = new MemoryStream())
        {
            inStream.Write(imageData, 0, imageData.Length);

            using (System.Drawing.Image image = Bitmap.FromStream(inStream))
            {
                Size thumbSize = GetThumbSize(new Size(image.Width, image.Height), size);

                //do not make image bigger
                if (thumbSize.Equals(image.Size) || (image.Width < size.Width || image.Height < size.Height))
                {
                    //if no shrinking is ocurring, return the original bytes
                    return imageData;
                }
                else
                {
                    using (System.Drawing.Image thumb = image.GetThumbnailImage(thumbSize.Width, thumbSize.Height, thumbnailCallback, IntPtr.Zero))
                    {

                        using (MemoryStream outStream = new MemoryStream())
                        {
                            thumb.Save(outStream, thumb.RawFormat);

                            return outStream.ToArray();
                        }
                    }
                }
            }
        }

    }

这一行抛出错误:
thumb.Save(outStream, thumb.RawFormat);

有任何想法吗?谢谢您的帮助!

最佳答案

我认为问题可能出在原始图像的编码上。 IIRC, Save(stream, format) 导致调用 Save(stream, encoder, params),编码器取自格式;在您的情况下,这是图像的原始格式。

根据 Save method 的社区内容,某些格式无法很好地转换为合适的编码器。

我建议你自己指定编码器,使用一些标准格式,比如 PNG。

尝试:

thumb.Save(outStream, ImageFormat.Png, null); // optionally add encoder parameters here, like quality or luminescence

关于bytearray - 创建缩略图,然后转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/894098/

相关文章:

c# - System.Drawing.Icon 构造函数抛出 "Operation completed successfully"异常

database - 如何填充 h :graphicImage value with image content from database?

c - 如何在C中将结构转换为字节数组?

php - 使用 PHP GD 库调整大小和保存图像是 hell

css - Bootstrap - 高度不同时堆叠缩略图

css - <HTML> 缩略图翻转图像仅出现在屏幕左侧

c# - 使用 .NET Core 处理图像

C#:在 XML 中存储字节数组

Java String 到 byteArray 的转换问题

azure - Azure 应用服务现在支持 GDI+ (System.Drawing) 吗?