我有一段時間創建縮略圖,然後將它們轉換成一個字節數組。我已經嘗試了三種方法,並且全部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);
任何想法?謝謝您的幫助!
Microsoft Connect上的此問題:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 – 2009-09-14 19:11:05