System.Drawing.Graphicsクラスを使用して画像のサイズを変更すると、結果の画像の右と下の境界から1ピクセルが失われることがわかりました。これは私のコードまたは.Netの問題のどこかにあるバグですか?テストコード:
public static void Resize(string imagePath,int width) {
InterpolationMode[] interpolationModes = new InterpolationMode[]{InterpolationMode.Bicubic, InterpolationMode.Bilinear, InterpolationMode.Default, InterpolationMode.High,
InterpolationMode.HighQualityBicubic, InterpolationMode.HighQualityBilinear, InterpolationMode.Low, InterpolationMode.NearestNeighbor};
SmoothingMode[] smoothingModes = new SmoothingMode[]{SmoothingMode.AntiAlias, SmoothingMode.Default, SmoothingMode.HighQuality, SmoothingMode.HighSpeed,
SmoothingMode.None};
for(int i = 0; i < interpolationModes.Length; i++) {
for(int j = 0; j < smoothingModes.Length; j++) {
Resize(imagePath, width, interpolationModes[i], smoothingModes[j]);
}
}
}
public static void Resize(string imagePath,int width, InterpolationMode interpolationMode, SmoothingMode smoothingMode) {
Image imgPhoto = Image.FromFile(imagePath);
float percent = (float)width / (float)imgPhoto.Width;
int height = (int)(imgPhoto.Height * percent);
Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = interpolationMode;
grPhoto.SmoothingMode = smoothingMode;
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, width, height),
new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height ),
GraphicsUnit.Pixel);
grPhoto.Dispose();
string fileName = Path.GetFileName(imagePath);
string path = Path.GetDirectoryName(imagePath)+"\\resized";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
bmPhoto.Save(String.Format("{0}\\{1}_{2}_{3}", path, interpolationMode.ToString(), smoothingMode.ToString(),fileName));
}
ソース画像: ソース画像http://img110.imageshack.us/img110/4876/sampleaa2.jpg
結果: 結果http://img110.imageshack.us/img110/2050/resizedsamplesy4.png
PS私はInterpolationModeとSmoothingModeの既存のすべての組み合わせを試しました。それらのどれも許容できる結果を与えませんでした。