积极答复者
Graphics使用InterpolationMode.High缩放图像黑图的问题

问题
-
Graphics使用InterpolationMode.High将两张特定的图片缩放到特定大小时,得到的是一张黑图。而其他类似的图片却能正常缩放。
请问是什么原因?
原图是89.1kb(91276字节) 1028*768 96dpi*96dpi 24色深的png图片。
至于图片,因为不知道怎么上传附件,所以暂时不传。
缩放代码如下:
FileStream fileStream = new FileStream(@"C:\i0000440.png");//取得图片的文件流 Bitmap bitMap = new Bitmap(fileStream);//创建bitmap fileSream.Dispse(); int x = 0; int y = 0; int width = 548;//缩放后的图的高,1280缩小成这个特定的高度 int height = 329;//缩放后的图的宽,768缩小成这个特定的宽度 Bitmap bitMap2 = new Bitmap(width, height, PixelFormat.Format32bppArgb);//缩放后的新图 Graphics gImg = Graphics.FromImage(bitMap2); gImg.CompositingQuality = CompositingQuality.HighQuality; gImg.InterpolationMode = InterpolationMode.High;// 指定高质量插值法。 指定这个算法缩放图片后,得到的这张特定图片是黑图,而指定高质量的双三次插值法InterpolationMode.HighQualityBicubic也会生成黑图,而指定其他算法就没有问题。另外,缩放成其他大小也没有问题,比如width=547, height=328。 gImg.SmoothingMode = SmoothingMode.HighQuality; gImg.Clear(Color.Transparent); gImg.DrawImage(bitmap, x, y, width, height); bitMap2.Save((@"C:\i0000440-2.png");//保存新图的位置没有特殊要求 bitMap.Dispose(); gImg.Dispose(); bitMap2.Dispose();
答案
-
你好
试试下面的代码,注意把保存为jpeg 的地方改成png就可以了。有些代码可以不要,参考下。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace DoctaJonez.Drawing.Imaging
{
/// <summary>
/// Provides various image untilities, such as high quality resizing and the ability to save a JPEG.
/// </summary>
public static class ImageUtilities
{
/// <summary>
/// A quick lookup for getting image encoders
/// </summary>
private static Dictionary<string, ImageCodecInfo> encoders = null;
/// <summary>
/// A quick lookup for getting image encoders
/// </summary>
public static Dictionary<string, ImageCodecInfo> Encoders
{
//get accessor that creates the dictionary on demand
get
{
//if the quick lookup isn't initialised, initialise it
if (encoders == null)
{
encoders = new Dictionary<string, ImageCodecInfo>();
}
//if there are no codecs, try loading them
if (encoders.Count == 0)
{
//get all the codecs
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
{
//add each codec to the quick lookup
encoders.Add(codec.MimeType.ToLower(), codec);
}
}
//return the lookup
return encoders;
}
}
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
/// <summary>
/// Saves an image as a jpeg image, with the given quality
/// </summary>
/// <param name="path">Path to which the image would be saved.</param>
/// <param name="quality">An integer from 0 to 100, with 100 being the
/// highest quality</param>
/// <exception cref="ArgumentOutOfRangeException">
/// An invalid value was entered for image quality.
/// </exception>
public static void SaveJpeg(string path, Image image, int quality)
{
//ensure the quality is within the correct range
if ((quality < 0) || (quality > 100))
{
//create the error message
string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality. A value of {0} was specified.", quality);
//throw a helpful exception
throw new ArgumentOutOfRangeException(error);
}
//create an encoder parameter for the image quality
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
//get the jpeg codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
//create a collection of all parameters that we will pass to the encoder
EncoderParameters encoderParams = new EncoderParameters(1);
//set the quality parameter for the codec
encoderParams.Param[0] = qualityParam;
//save the image using the codec and the parameters
image.Save(path, jpegCodec, encoderParams);
}
/// <summary>
/// Returns the image codec with the given mime type
/// </summary>
public static ImageCodecInfo GetEncoderInfo(string mimeType)
{
//do a case insensitive search for the mime type
string lookupKey = mimeType.ToLower();
//the codec to return, default to null
ImageCodecInfo foundCodec = null;
//if we have the encoder, get it to return
if (Encoders.ContainsKey(lookupKey))
{
//pull the codec from the lookup
foundCodec = Encoders[lookupKey];
}
return foundCodec;
}
}
}
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Cookie Luo 2011年4月25日 2:47