0
A
回答
1
private Bitmap CropImage(Image originalImage, Rectangle sourceRectangle,
Rectangle? destinationRectangle = null)
{
if (destinationRectangle == null)
{
destinationRectangle = new Rectangle(Point.Empty, sourceRectangle.Size);
}
var croppedImage = new Bitmap(destinationRectangle.Value.Width,
destinationRectangle.Value.Height);
using (var graphics = Graphics.FromImage(croppedImage))
{
graphics.DrawImage(originalImage, destinationRectangle.Value,
sourceRectangle, GraphicsUnit.Pixel);
}
return croppedImage;
}
/// <summary>
/// Button click to choose an image and test
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCrop_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string imageFile = ofd.FileName;
Image img = new Bitmap(imageFile);
Rectangle source = new Rectangle(0, 0, 120, 20);
Image cropped = CropImage(img, source);
// Save cropped image here
cropped.Save(Path.GetDirectoryName(imageFile) + "\\croppped." + Path.GetExtension(imageFile));
}
}
相关问题
- 1. 设置图像的宽度和高度
- 2. CSS如何设置图像而不设置宽度和高度
- 3. 如何设置位图的图像高度和宽度属性
- 4. 设置图像高度=宽度(不DIV)
- 5. 如何设置colorbox iframe中图像的高度和宽度?
- 6. 如何设置标记包含图像的宽度和高度
- 7. 如何设置在J2ME(Java)的图像高度和宽度
- 8. 如何将宽度和高度设置为可拖动图像?
- 9. 如何使用CSS设置图像宽度和高度100%?
- 10. 如何设置图像的高度和宽度?
- 11. 如何在c#中设置宽度和高度位图?
- 12. 如何在Jcrop中设置自定义高度和宽度后获取图像的原始高度和宽度
- 13. 如何设置imageurl并在C#中动态获取图像高度和宽度
- 14. 重置图像的宽度和高度设置通过css
- 15. 获取没有设置高度/宽度的图像的高度/宽度
- 16. 设置图像DPI与高度/宽度有关C#
- 17. 在代码隐藏设置图像宽度/高度 - ASP.NET,C#
- 18. Android:如何设置ImageButton的src图像的高度/宽度?
- 19. 如何设置所有图像的iFrame宽度高度相同
- 20. 如何在设置高度后获取图像的宽度
- 21. 如何根据父级的宽度和高度来设置FontAwesome图标的宽度,高度和位置?
- 22. Typo3:如何设置像宽度,高度和ALT等图像属性?
- 23. 图像处理宽度和高度设置
- 24. 将div设置为子图像的宽度和高度?
- 25. 动态设置图像的宽度和高度与PHP等
- 26. SpreadSheetAddImage - 添加设置宽度和高度的图像
- 27. PHP图像大小调整 - 设置高度和宽度自动
- 28. 设置宽度和图像高度经由worksheet.insert_image
- 29. 设置jquery弹出大图像的宽度和高度
- 30. Fancybox设置一些图像的高度和宽度属性
http://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c –
会尝试这个功能你想裁剪它还是只显示它的一部分? –
@TalhaTalipAçıkgöz是 – amir