创建一个缩略图,然后转换为字节数组 [英] Create a thumbnail and then convert to byte array

查看:104
本文介绍了创建一个缩略图,然后转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建缩略图然后将它们转换为字节数组方面,我花了很多时间.我尝试了三种方法,但全部3次都出现错误.

I'm having a heck of a time with creating thumbnails and then converting them into a byte array. I've tried three methods, and all 3 times I get an error.

首先使用Bitmap.GetThumbnailImage,我以前使用过它,然后直接将其保存到Response.OutputStream

The first was using Bitmap.GetThumbnailImage, which I have used in the past and then saved directly into Response.OutputStream

第二个是将System.Drawing.Graphics与DrawImage()一起使用.还是不行.

The second was using System.Drawing.Graphics with DrawImage(). Still no go.

第三个只是创建一个新的位图,传入旧的位图,然后设置新的大小.同样的错误.

The third was just to create a new bitmap, pass in the old bitmap, and set the new size. Same error.

值不能为空.
参数名称:编码器

说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.ArgumentNullException:值不能为null.
参数名称:编码器

源错误:
当前Web请求的执行期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息.

堆栈跟踪:
[ArgumentNullException:值不能为null.
参数名称:编码器]
System.Drawing.Image.Save(流,ImageCodecInfo编码器,EncoderParameters编码器参数)+615244

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,它只是一种获取图像大小和最大拇指大小,然后计算实际大小以保持宽高比的方法.

Here is the code for my method. Maybe someone will see what I'm doing wrong. In case you aren't sure about GetThumbSize, it's simply a method that takes in the image size and the maximum thumb size and then computes an actual size to preserve the aspect ratio.

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

有什么想法吗?感谢您的帮助!

Any ideas? Thanks for the help!

推荐答案

我认为问题可能出在原始图像的编码上. IIRC,Save(流,格式)导致对Save(流,编码器,参数)的调用,编码器取自格式;您的情况就是图片的原始格式.

I think the problem may be the original image's encoding. IIRC, Save(stream, format) results in a call to Save(stream, encoder, params), with the encoder being taken from the format; which in your case is the original format of the image.

根据保存方法的社区内容,某些格式不能很好地转换为合适的编码器.

According to the Community Content for the Save method, some formats will not translate well into an appropriate encoder.

我建议您使用一些标准格式(例如PNG)自己指定编码器.

I would suggest you specify the encoder yourself, using some standard format like PNG.

尝试:

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

这篇关于创建一个缩略图,然后转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆