使用System.Drawing制作选择工具,并在vb.net中裁剪图像
如果我想在 VB.net 中裁剪图像,我该怎么做?我试图让用户拖出他们想要的框(system.drawing.rectangle),它会自动删除框周围的边缘。
我的第一个问题主要是我无法使 system.drawing.rectangle 可见。它根本不显示,我正在以编程方式设置其位置和高度,但什么也没有显示。我知道我可能缺少一些相当明显的东西......但我似乎找不到它。
然而,我更大的问题在于裁剪本身。我根本找不到任何裁剪方法。有没有我失踪的隐藏的?还是我必须自己编码?我该怎么做呢?它应该能够输出到位图图像对象。
感谢您的帮助,我很惊讶以前没有人问过这个问题......
If i wanted to crop an image in VB.net, how would I go about doing it? I am trying to let the user drag out the box they want (system.drawing.rectangle), and it will automatically remove the edges surrounding the box.
My first problem is primarily the fact that I cannot make the system.drawing.rectangle visible. It is not displaying at all, I am setting its location and height programmatically, but nothing is showing up. I know there is probably something fairly obvious I am missing...but I cannot seem to find it.
My larger issue, however, lies with the cropping itself. I cannot find any crop methods, at all. Is there a hidden one I am missing? Or must I code it myself? How would I go about doing this? It ought to be able to output to a bitmap image object.
Thanks for the help, I am surprised this hasn't been asked on here before....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于您的第一个问题:矩形本身不可见。您必须使用 Graphics 对象的 DrawRectangle(...) 方法将其绘制在画布上。为了绘制选择工具,您需要设置 Pen 对象的 DashCap 和 DashPattern 属性。
要“裁剪”图像,您基本上需要获取由较小矩形描绘的较大图像的部分,并将其转换为新的位图。这可以使用 Graphics 对象的 DrawImage(...) 方法的 30 个重载之一来完成。您可以将裁剪部分保留在其原始尺寸(导致位图比原始尺寸小),也可以将其“放大”到原始图像的尺寸。如果您采用后一种方法,通常最好将 Graphics 对象的 InterpolationMode 属性设置为 HighQualityBicubic (无论如何,我认为就是这样),因为默认的 InterpolationMode 非常糟糕。
在 .Net 中有多种不同的渲染图像的方法 - 如果您发布一些代码以及对您遇到的确切问题的解释,这可能会有所帮助。
这是另一个答案,其中包含以下链接一个示例应用程序(以及 C# 源代码,抱歉)可以帮助您入门。
Regarding your first problem: a Rectangle isn't by itself visible. You have to draw it on your canvas using the Graphics object's DrawRectangle(...) method. For drawing a selection tool, you'll want to set your Pen object's DashCap and DashPattern properties.
To "crop" an image, you basically want to take the portion of a larger image delineated by a smaller Rectangle, and turn it into a new Bitmap. This can be done using one of the 30 overloads of the Graphics object's DrawImage(...) method. You can either keep the cropped portion in its original dimensions (resulting in a smaller Bitmap than your original), or you can "blow it up" to something like the original image's size. If you do the latter approach, it is usually a good idea to set your Graphics object's InterpolationMode property to HighQualityBicubic (I think that's the one, anyway), since the default InterpolationMode is pretty crappy.
There are a number of different ways of rendering images in .Net - it might help if you posted some of your code, along with an explanation of the exact problems you're running into.
Here is another answer with a link to a sample app (and source code in C#, sorry) that may help you get started.
CodeProject 上有很多关于这些主题的文章:
选择您最喜欢的风格(尽管我鼓励您查看 C# 项目 - 转换应该不会太难)。
VB
使用 vb.net 裁剪图像并调整图像大小
C#
裁剪图像
易于使用的图像调整大小和裁剪控件
使用 C# 进行图像处理(请参阅裁剪部分 - 我能够在我的一个项目中使用此代码)
WPF/C#
WPF 交互式图像裁剪控件
类似 Photoshop 的 WPF 裁剪装饰器
There are a number of articles on these topics on CodeProject:
Pick your favorite flavor (though I encourage you to check out the C# projects - it shouldn't be too hard to convert).
VB
Image Cropping with Image Resizing Using vb.net
C#
Cropping Images
An Easy to Use Image Resizing and Cropping Control
Image Processing using C# (see the Cropping section - I was able to use this code in one of my projects)
WPF/C#
WPF Interactive Image Cropping Control
A Photoshop-like Cropping Adorner for WPF