【发布时间】:2009-12-06 04:04:46
【问题描述】:
我需要调整 bmp 的大小,就像在 MS Paint 中调整大小一样 - 没有抗锯齿。 有人知道如何在 c# 或 vb.net 中执行此操作吗?
【问题讨论】:
-
WPF 的新功能通常比旧的 System.Drawing 更快更好。查看stackoverflow.com/questions/754168/…
我需要调整 bmp 的大小,就像在 MS Paint 中调整大小一样 - 没有抗锯齿。 有人知道如何在 c# 或 vb.net 中执行此操作吗?
【问题讨论】:
您可以使用Image.GetThumbnailImage 方法。我不知道它有抗锯齿功能。
编辑:自从我最近在一个项目中使用它以来,我一直在考虑缩略图。但你只是要求调整大小。这种方法可能不会产生高质量的大尺寸调整。
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
【讨论】:
您可以将图形插值模式设置为最近邻,然后使用drawimage调整它的大小而不进行抗锯齿。 (请原谅我的 vb :-))
Dim img As Image = Image.FromFile("c:\jpg\1.jpg")
Dim g As Graphics
pic1.Image = New Bitmap(180, 180, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
g = Graphics.FromImage(pic1.Image)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(img, 0, 0, pic1.Image.Width, pic1.Image.Height)
【讨论】:
How to: Copy Images 来自 MSDN。
油漆只是将图像切掉,不是吗?该页面上的示例包含您需要的工具。
【讨论】:
// ********************************************** ScaleBitmap
/// <summary>
/// Scale a bitmap by a scale factor, growing or shrinking
/// both axes, maintaining the aspect ratio
/// </summary>
/// <param name="inputBmp">
/// Bitmap to scale
/// </param>
/// <param name="scale_factor">
/// Factor by which to scale
/// </param>
/// <returns>
/// New bitmap containing the original image, scaled by the
/// scale factor
/// </returns>
/// <citation>
/// A Bitmap Manipulation Class With Support For Format
/// Conversion, Bitmap Retrieval from a URL, Overlays, etc.,
/// Adam Nelson, The Code Project, September 2003.
/// </citation>
private Bitmap ScaleBitmap ( Bitmap bitmap,
float scale_factor )
{
Graphics g = null;
Bitmap new_bitmap = null;
Rectangle rectangle;
int height = ( int ) ( ( float ) bitmap.Size.Height *
scale_factor );
int width = ( int ) ( ( float ) bitmap.Size.Width *
scale_factor );
new_bitmap = new Bitmap ( width,
height,
PixelFormat.Format24bppRgb );
g = Graphics.FromImage ( ( Image ) new_bitmap );
g.InterpolationMode = InterpolationMode.High;
g.ScaleTransform ( scale_factor, scale_factor );
rectangle = new Rectangle ( 0,
0,
bitmap.Size.Width,
bitmap.Size.Height );
g.DrawImage ( bitmap,
rectangle,
rectangle,
GraphicsUnit.Pixel );
g.Dispose ( );
return ( new_bitmap );
}
【讨论】: