图像缩放问题 [英] Problem of scaling images
问题描述
http://img29.imageshack.us/img29/3247/thisjs.png
我有缩放图像的问题.
例如,当我将尺寸为1x7的图像缩放为82x7时,
它将按比例缩放,但具有渐变色.
我该怎么解决?
我正在使用图形方法显示图像.
并且使用Graphics InterpolationMode无法解决此问题.
这是我的代码:
http://img29.imageshack.us/img29/3247/thisjs.png
I have a question of scaling images.
When I scale an image with 1x7 in size to 82x7 for example,
it will be scaled but with a gradient of colour.
How can I solve it?
I am using graphic method to display the image.
And using the Graphics InterpolationMode cannot solve this problem.
Here is my code:
Bitmap top_left = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_top_left@n.tga");
Bitmap top_center = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_top_center@n.tga");
Bitmap top_right = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_top_right@n.tga");
Bitmap center_left = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_center_left@n.tga");
Bitmap center_center = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_center_center@n.tga");
Bitmap center_right = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_center_right@n.tga");
Bitmap bottom_left = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_bottom_left@n.tga");
Bitmap bottom_center = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_bottom_center@n.tga");
Bitmap bottom_right = TargaImage.LoadTargaImage(Application.StartupPath + "\\cstrike\\resource\\control\\textentry\\output_default_bottom_right@n.tga");
Rectangle tlr1 = new Rectangle(0, 0, top_left.Width, top_left.Height);
e.Graphics.DrawImage(top_left, tlr1);
Rectangle tcr1 = new Rectangle(top_left.Width, 0, (ClientSize.Width - (top_left.Width + top_right.Width)), top_left.Height);
e.Graphics.DrawImage(top_center, tcr1);
Rectangle trr1 = new Rectangle((ClientSize.Width - top_left.Width), 0, top_right.Width, top_right.Height);
e.Graphics.DrawImage(top_right, trr1);
Rectangle clr1 = new Rectangle(0, top_left.Height, center_left.Width, (ClientSize.Height - (top_left.Height + bottom_left.Height)));
e.Graphics.DrawImage(center_left, clr1);
Rectangle ccr1 = new Rectangle(center_left.Width, top_left.Height, (ClientSize.Width - (center_left.Width + center_right.Width)), (ClientSize.Height - (top_left.Height + bottom_left.Height)));
e.Graphics.DrawImage(center_center, ccr1);
Rectangle crr1 = new Rectangle((ClientSize.Width - center_left.Width), top_left.Height, center_right.Width, (ClientSize.Height - (top_left.Height + bottom_right.Height)));
e.Graphics.DrawImage(center_right, crr1);
Rectangle blr1 = new Rectangle(0, (ClientSize.Height - top_left.Height), center_left.Width, bottom_left.Width);
e.Graphics.DrawImage(bottom_left, blr1);
Rectangle bcr1 = new Rectangle(bottom_left.Width, (ClientSize.Height - top_left.Height), (ClientSize.Width - (bottom_left.Width + bottom_right.Width)), bottom_center.Height);
e.Graphics.DrawImage(bottom_center, bcr1);
Rectangle brr1 = new Rectangle((ClientSize.Width - bottom_left.Width), (ClientSize.Height - top_left.Height), bottom_right.Width, bottom_right.Height);
e.Graphics.DrawImage(bottom_right, brr1);
推荐答案
使用扩展方法,即可实现.这是示例
Using extension method, you can achieve it. Here is the sample
public static Image GetImageHiQualityResized(this Image image, int width, int height)
{
var thumb = new Bitmap(width, height);
using (var g = Graphics.FromImage(thumb))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
return thumb;
}
}
using(var original = Image.FromFile(@"C:\myimage.jpg"))
using(var thumb = image.GetImageHiQualityResized(120, 80))
{
thumb.Save(@"C:\mythumb.png", ImageFormat.Png);
}
有关MSDN参考,请访问: http://msdn.microsoft.com/en-us/library/84767bxk. aspx [^ ]
MSDN reference is available at: http://msdn.microsoft.com/en-us/library/84767bxk.aspx[^]
我已经解决了.
只需将这两行相加即可完成.
I have solved it.
Just adding this two lines then finish.
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
这篇关于图像缩放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!