从裁剪图像创建新图像

发布于 2024-08-05 22:03:30 字数 426 浏览 2 评论 0原文

我目前正在尝试裁剪图像,然后保存新图像。我有原始图像、该图像上我想要裁剪的位置的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-08-12 22:03:30

尝试使用位图的克隆功能:

Bitmap newImage = originalBitmap.Clone(new RectangleF(x, y, width, height),  
                                       System.Drawing.Imaging.PixelFormat.Format32bppArgb);
newImage.Save(newFilePath);

try using Clone function of Bitmap:

Bitmap newImage = originalBitmap.Clone(new RectangleF(x, y, width, height),  
                                       System.Drawing.Imaging.PixelFormat.Format32bppArgb);
newImage.Save(newFilePath);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文