将图像的一部分渲染为Bitmap C#Winforms

时间:2021-08-14 00:31:39

I'm working on a Map Editor for an XNA game I'm designing in my free time. The pieces of art used in the map are stored on a single texture and rectangles are stored with coordinates and widths etc.

我正在为我在空闲时间设计的XNA游戏的地图编辑器工作。地图中使用的艺术品存储在单个纹理上,矩形存储有坐标和宽度等。

In the winforms application I can add segments by selecting the segment I want from a listbox, which is populated from the array of possible segments.

在winforms应用程序中,我可以通过从列表框中选择我想要的段来添加段,列表框是从可能段的数组中填充的。

Problem is I would like to be able to show a preview of the segment selected and, since it is stored on a common texture, I cant simply set a picturebox to display the image.

问题是我希望能够显示所选片段的预览,因为它存储在一个共同的纹理上,我不能简单地设置一个图片框来显示图像。

Is there anyway of using the rectangle information (.x, .y, .width, .height) to display only the section of the image in a picturebox, or to blit the section to a bitmap and display that?

无论如何使用矩形信息(.x,.y,.width,.height)只显示图片框中的图像部分,或者将部分blit到位图并显示?

Many Thanks

Michael Allen

2 个解决方案

#1


You probably want to look into the GDI library. Using the Image or Bitmap object and the Graphics.DrawImage() together will get what you're looking for.

您可能想要查看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);
}

You also might be interested in using XNA within your WinForm instead of using PictureBoxes and GDI. It's not 100% supported yet, but a tutorial on that can be found here.

您可能也有兴趣在WinForm中使用XNA而不是使用PictureBoxes和GDI。它还没有100%支持,但是可以在这里找到关于它的教程。

#2


You can use Graphics.DrawImage() and that will accept a Rectangle.

您可以使用Graphics.DrawImage(),它将接受一个Rectangle。

#1


You probably want to look into the GDI library. Using the Image or Bitmap object and the Graphics.DrawImage() together will get what you're looking for.

您可能想要查看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);
}

You also might be interested in using XNA within your WinForm instead of using PictureBoxes and GDI. It's not 100% supported yet, but a tutorial on that can be found here.

您可能也有兴趣在WinForm中使用XNA而不是使用PictureBoxes和GDI。它还没有100%支持,但是可以在这里找到关于它的教程。

#2


You can use Graphics.DrawImage() and that will accept a Rectangle.

您可以使用Graphics.DrawImage(),它将接受一个Rectangle。