我需要像MS Paint中的调整大小一样调整bmp大小,也就是没有抗锯齿。 有人知道如何在c#或vb.net中实现这一点吗?
您可以将图形插值模式设置为最近邻,然后使用drawimage进行调整大小而不进行抗锯齿处理。
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)
Image.GetThumbnailImage
方法。我不知道它是否进行抗锯齿处理。
编辑:我考虑的是缩略图,因为最近在项目中使用了这个方法。但是您只是要求一般的调整大小。该方法可能无法获得良好的大尺寸调整质量。
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
// ********************************************** 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 );
}