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