最近做了一些关于画图方面的东西, 分享一下, Winform 截图, 此文而屏幕截图, 只是截图片的其中一部分,
方法1.
/// <summary>
/// Crop Image
/// </summary>
/// <param name="image">Original image</param>
/// <param name="targetSize"></param>
/// <param name="x">x cod</param>
/// <param name="y">y cod</param>
/// <returns>After crop image</returns>
public static Bitmap Crop(Image image, Size targetSize, int x, int y)
{
try
{
Bitmap bmp = new Bitmap(targetSize.Width, targetSize.Height);//, PixelFormat.Format24bppRgb);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, targetSize.Width, targetSize.Height),
x, y, targetSize.Width, targetSize.Height, GraphicsUnit.Pixel);
gfx.Dispose();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
/// Crop Image
/// </summary>
/// <param name="image">Original image</param>
/// <param name="targetSize"></param>
/// <param name="x">x cod</param>
/// <param name="y">y cod</param>
/// <returns>After crop image</returns>
public static Bitmap Crop(Image image, Size targetSize, int x, int y)
{
try
{
Bitmap bmp = new Bitmap(targetSize.Width, targetSize.Height);//, PixelFormat.Format24bppRgb);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, targetSize.Width, targetSize.Height),
x, y, targetSize.Width, targetSize.Height, GraphicsUnit.Pixel);
gfx.Dispose();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
方法2.
private static Image CropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
两种方法出来的效果都一样,只不过方法1可以直接set 分辨率,用起来都比较容易.