这很容易:
- 用裁剪后的大小创建一个新的
Bitmap
对象。 - 使用
Graphics.FromImage
为新的位图创建Graphics
对象。 - 使用
DrawImage
方法使用X和Y负坐标将图像绘制到位图上。
0
这很容易:
Bitmap
对象。 Graphics.FromImage
为新的位图创建Graphics
对象。 DrawImage
方法使用X和Y负坐标将图像绘制到位图上。 0
如果您使用的是AForge.NET :
using(var croppedBitmap = new Crop(new Rectangle(10, 10, 10, 10)).Apply(bitmap))
{
// ...
}
0
比接受的答案简单的是:
public static Bitmap cropAtRect(this Bitmap b, Rectangle r)
{
Bitmap nb = new Bitmap(r.Width, r.Height);
Graphics g = Graphics.FromImage(nb);
g.DrawImage(b, -r.X, -r.Y);
return nb;
}
并且避免了最简单答案的“ 内存不足 ”异常风险。
编辑 :我发现使用Bitmap.Save
或Paint.exe保存的PNG很好,但是对于使用例如Paint Shop Pro 6保存的PNG则失败-内容被替换了。添加GraphicsUnit.Pixel
会得出不同的错误结果。这些失败的PNG也许只是错误的。
0
您可以使用Graphics.DrawImage
将裁剪的图像从位图绘制到图形对象上。
Rectangle cropRect = new Rectangle(...);
Bitmap src = Image.FromFile(fileName) as Bitmap;
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using(Graphics g = Graphics.FromImage(target))
{
g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
cropRect,
GraphicsUnit.Pixel);
}
0
有一个用于C#包装的开源程序,托管在Codeplex上,称为Web Image Cropping。
注册控件
<%@ Register Assembly="CS.Web.UI.CropImage" Namespace="CS.Web.UI" TagPrefix="cs" %>
调整大小
<asp:Image ID="Image1" runat="server" ImageUrl="images/328.jpg" />
<cs:CropImage ID="wci1" runat="server" Image="Image1"
X="10" Y="10" X2="50" Y2="50" />
在后面的代码中裁剪 -例如,单击按钮时调用Crop方法;
wci1.Crop(Server.MapPath("images/sample1.jpg"));
0
我一直在寻找一个简单且快速的功能,而没有其他库来完成这项工作。我尝试使用Nicks解决方案 ,但是我需要29.4秒的时间才能“提取” 1195张地图集文件的图像。因此,后来我以这种方式进行管理,并且需要2.43秒才能完成相同的工作。也许这会有所帮助。
// content of the Texture class
public class Texture
{
//name of the texture
public string name { get; set; }
//x position of the texture in the atlas image
public int x { get; set; }
//y position of the texture in the atlas image
public int y { get; set; }
//width of the texture in the atlas image
public int width { get; set; }
//height of the texture in the atlas image
public int height { get; set; }
}
Bitmap atlasImage = new Bitmap(@"C:\somepicture.png");
PixelFormat pixelFormat = atlasImage.PixelFormat;
foreach (Texture t in textureList)
{
try
{
CroppedImage = new Bitmap(t.width, t.height, pixelFormat);
// copy pixels over to avoid antialiasing or any other side effects of drawing
// the subimages to the output image using Graphics
for (int x = 0; x < t.width; x++)
for (int y = 0; y < t.height; y++)
CroppedImage.SetPixel(x, y, atlasImage.GetPixel(t.x + x, t.y + y));
CroppedImage.Save(Path.Combine(workingFolder, t.name + ".png"), ImageFormat.Png);
}
catch (Exception ex)
{
// handle the exception
}
}
0
这是裁剪图像的简单示例
public Image Crop(string img, int width, int height, int x, int y)
{
try
{
Image image = Image.FromFile(img);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmp.SetResolution(80, 60);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
// Dispose to free up resources
image.Dispose();
bmp.Dispose();
gfx.Dispose();
return bmp;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
0
查看此链接: http : //www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-reizing
private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
return bmpImage.Clone(cropArea, bmpImage.PixelFormat);
}
0
使用bmp.SetResolution(image.HorizontalResolution,image .VerticalResolution);
即使您在此处实现最佳答案,也可能需要这样做,特别是当您的图像非常好并且分辨率不完全是96.0时
我的测试示例:
static Bitmap LoadImage()
{
return (Bitmap)Bitmap.FromFile( @"e:\Tests\d_bigImage.bmp" ); // here is large image 9222x9222 pixels and 95.96 dpi resolutions
}
static void TestBigImagePartDrawing()
{
using( var absentRectangleImage = LoadImage() )
{
using( var currentTile = new Bitmap( 256, 256 ) )
{
currentTile.SetResolution(absentRectangleImage.HorizontalResolution, absentRectangleImage.VerticalResolution);
using( var currentTileGraphics = Graphics.FromImage( currentTile ) )
{
currentTileGraphics.Clear( Color.Black );
var absentRectangleArea = new Rectangle( 3, 8963, 256, 256 );
currentTileGraphics.DrawImage( absentRectangleImage, 0, 0, absentRectangleArea, GraphicsUnit.Pixel );
}
currentTile.Save(@"e:\Tests\Tile.bmp");
}
}
}
0
如何编写将在C#中裁剪图像的应用程序?