将图像的一部分渲染到位图C#Winforms [英] Render a section of an image to a Bitmap C# Winforms

查看:117
本文介绍了将图像的一部分渲染到位图C#Winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己在业余时间设计的XNA游戏开发地图编辑器.地图中使用的艺术品存储在单个纹理上,矩形存储有坐标和宽度等.

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

问题是我希望能够显示所选片段的预览,并且由于它存储在通用纹理上,因此我不能简单地设置图片框来显示图像.

无论如何,是否使用矩形信息(.x,.y,.width,.height)仅在图片框中显示图像的一部分,或者将该部分变成位图并显示出来?

非常感谢

迈克尔·艾伦

解决方案

您可能想研究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%,但可以在此处上找到有关该教程的信息.

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.

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.

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?

Many Thanks

Michael Allen

解决方案

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.

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.

这篇关于将图像的一部分渲染到位图C#Winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆