我在与创建缩略图,然后将它们转换成一个字节数组的时间赫克。我已经试过三种方法,所有3次,我得到一个错误。

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

在第二使用System.Drawing.Graphics用的DrawImage()。仍然没有去。

在第三只是创建一个新的位图,通在旧的位图,并设置新的大小。同样的错误。

  

值不能为空。结果   参数名称:编码器搜索结果   说明:在当前Web请求的执行过程中发生未处理的异常。请检查堆栈跟踪有关该错误它起源于代码的详细信息和。搜索结果   异常详细信息:System.ArgumentNullException:值不能为空结果。   参数名称:编码器搜索结果   源错误:点击   在当前web请求的执行过程中生成未处理的异常。有关异常原因和发生位置的信息可以使用异常堆栈跟踪下面来识别。搜索结果   堆栈跟踪:点击   [ArgumentNullException:值不能为空结果。   参数名称:编码器]点击      System.Drawing.Image.Save(流流,编码器ImageCodecInfo,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,保存(流格式)的结果是呼叫保存(流,编码器,则params),与编码器从格式正在采取;而你的情况是图像的原始格式。

根据该社区内容为保存方法时,一些格式不会很好地翻译成一个适当的编码器。

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

尝试:

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

其他提示

如果你正在尝试做的是保存在一个Raw格式,你可以尝试以下方法,在我的情况下,它的工作原理,当原始图像格式是一个支持的:

try
{
   thumb.Save(outStream, img.RawFormat);
}
catch (System.ArgumentNullException)
{
   thumb.Save(outStream, ImageFormat.Png);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top