将图像的一部分渲染为Bitmap C#Winforms
-
10-07-2019 - |
题
我正在为我在空闲时间设计的XNA游戏制作地图编辑器。地图中使用的艺术品存储在单个纹理上,矩形存储有坐标和宽度等。
在winforms应用程序中,我可以通过从列表框中选择我想要的段来添加段,列表框是从可能段的数组中填充的。
问题是我希望能够显示所选片段的预览,因为它存储在一个共同的纹理上,所以我不能简单地设置一个图片框来显示图像。
无论如何使用矩形信息(.x,.y,.width,.height)只显示图片框中的图像部分,或者将部分blit到位图并显示?
非常感谢
迈克尔艾伦解决方案
您可能想要查看GDI库。一起使用Image或Bitmap对象和Graphics.DrawImage()将获得您正在寻找的东西。
private void DrawImageRectRect(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle(100, 100, 450, 150);
// Create rectangle for source image.
Rectangle srcRect = new Rectangle(50, 50, 150, 150);
GraphicsUnit units = GraphicsUnit.Pixel;
// Draw image to screen.
e.Graphics.DrawImage(newImage, destRect, srcRect, units);
}
您可能也有兴趣在WinForm中使用XNA而不是使用PictureBoxes和GDI。它尚未100%支持,但可以在此处找到相关教程。
其他提示
您可以使用Graphics.DrawImage(),它将接受一个Rectangle。
不隶属于 StackOverflow