var filename = @"c:\personal\images\horizon.png";
var img = Image.FromFile(filename);
var rect = new Rectangle(new Point(0, 0), img.Size);
var cloned = new Bitmap(img).Clone(rect, img.PixelFormat);
var bitmap = new Bitmap(cloned, new Size(50, 50));
cloned.Dispose();
204
只有这个样本工作没有问题:
var crop = new Rectangle(0, y, bitmap.Width, h);
var bmp = new Bitmap(bitmap.Width, h);
var tempfile = Application.StartupPath+"\\"+"TEMP"+"\\"+Path.GetRandomFileName();
using (var gr = Graphics.FromImage(bmp))
{
try
{
var dest = new Rectangle(0, 0, bitmap.Width, h);
gr.DrawImage(image,dest , crop, GraphicsUnit.Point);
bmp.Save(tempfile,ImageFormat.Jpeg);
bmp.Dispose();
}
catch (Exception)
{
}
}
// 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
}
}
14 回答
您可以使用Graphics.DrawImage从位图中将裁剪后的图像绘制到图形对象上 .
看看这个链接:http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing [<== Link is DEAD]
比接受的答案更简单的是:
它避免了最简单答案的“ Out of memory ”异常风险 .
EDIT :我发现
Bitmap.Save
或Paint.exe保存的PNG很好,但是由于例如保存了PNG而失败了 . Paint Shop Pro 6 - 内容被取代 . 添加GraphicsUnit.Pixel
会产生不同的错误结果 . 也许只是这些失败的PNG是错误的 .这可能是必要的,即使你在这里实现最佳答案,特别是如果你的图像真的很棒,分辨率不是96.0
我的测试示例:
这很容易:
使用裁剪尺寸创建新的
Bitmap
对象 .使用
Graphics.FromImage
为新位图创建Graphics
对象 .使用
DrawImage
方法将图像绘制到具有负X和Y坐标的位图上 .如果你正在使用AForge.NET:
这是一个关于裁剪图像的简单示例
有一个C#包装器,它是开源的,托管在Codeplex上,名为 Web Image Cropping
Register the control
<%@ Register Assembly="CS.Web.UI.CropImage" Namespace="CS.Web.UI" TagPrefix="cs" %>
Resizing
Cropping in code behind - 单击按钮时调用裁剪方法;
wci1.Crop(Server.MapPath("images/sample1.jpg"));
假设您想要拍摄图像文件(JPEG,BMP,TIFF等)并裁剪它然后将其保存为较小的图像文件,我建议使用具有.NET API的第三方工具 . 以下是我喜欢的一些流行的:
LeadTools
Accusoft Pegasus Snowbound Imaging SDK
在C#中剪切图像非常容易 . 然而,做这些事情你将如何管理你的图像裁剪将会有点困难 .
以下示例是如何在C#中裁剪图像的方法 .
只有这个样本工作没有问题:
这是另一种方式 . 就我而言,我有:
2个数字更新控件(称为LeftMargin和TopMargin)
1图片框(pictureBox1)
我调用生成的1个按钮
在C:\ imagenes \ myImage.gif上
1张图片
在按钮内我有这个代码:
我在使用C#的Visual Studio 2012中尝试过它 . 我发现了这个解决方案page
这是在github上运行的演示
https://github.com/SystematixIndore/Crop-SaveImageInCSharp
用于上传和裁剪的C#代码逻辑 .
我正在寻找一个简单而快速的功能,没有额外的库来完成这项工作 . 我试过了Nicks solution,但我需要29,4秒到"extract" 1195张 Map 集文件的图像 . 所以后来我以这种方式管理,需要2,43秒来完成同样的工作 . 也许这会有所帮助 .