从裁剪图像创建新图像
我目前正在尝试裁剪图像,然后保存新图像。我有原始图像、该图像上我想要裁剪的位置的 x 和 y 坐标,以及新裁剪图像的宽度和高度。
这是我的代码:
Bitmap originalBitmap = new Bitmap(filePath);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
g.DrawImage(originalBitmap, x, y, width, height);
newImage.Save(newFilePath);
但是当图像实际保存时,它是一个具有正确高度和宽度的小图像,但完全是空的。
我确信我只是在这里错过了一个技巧,或者完全误解了某些东西(或两者!),所以任何帮助将不胜感激!
I'm currently trying to crop an image, and then save the new image. I have the original image, the x and y co-ordinates of where on that image I want the cropping to being, and the width and height of the new cropped image.
Here is my code:
Bitmap originalBitmap = new Bitmap(filePath);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
g.DrawImage(originalBitmap, x, y, width, height);
newImage.Save(newFilePath);
But when the image is acutally saved, it's a small image of the correct height and width, but is completely empty.
I'm sure I'm just missing a trick here, or completely misunderstanding something (or both!), so any help at all would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用位图的克隆功能:
try using Clone function of Bitmap: