在C#Winforms中将多个图像渲染为位图

时间:2021-11-28 20:54:44

I'm developing a set of applications for use creating games in XNA. Using Graphics.drawImage I can easily draw a preview image from an XNA texture2D object.

我正在开发一套用于在XNA中创建游戏的应用程序。使用Graphics.drawImage我可以轻松地从XNA texture2D对象绘制预览图像。

Each object, eg Character, Map etc, is made up of a List of parts, each part storing information such as position rotation and texture source. The next step is to render a preview of the entire object instead of just a part.

每个对象,例如字符,地图等,由部件列表组成,每个部件存储诸如位置旋转和纹理源之类的信息。下一步是渲染整个对象的预览,而不仅仅是一个部分。

How would I go about this? Can I just treat Graphics.drawImage as a regular XNA draw call and render the section of the object I want to a bitmap by looping through the List and drawing each item to the bitmap in position and in order? Or does each graphics.DrawImage call destroy the Bitmap it draws to?

我怎么会这样呢?我可以将Graphics.drawImage视为常规的XNA绘制调用,并通过循环遍历List并将每个项目绘制到位图和顺序中的位图来渲染我想要的位图对象的部分吗?或者每个graphics.DrawImage调用是否会破坏它绘制的Bitmap?

2 个解决方案

#1


Is this what you're looking for?

这是你在找什么?

   Bitmap bmp = new Bitmap(100, 100);
   Graphics g = Graphics.FromImage(bmp);
   g.DrawImage(Properties.Resources.Foo);
   Bitmap bar = Properties.Resources.Bar;
   bar.MakeTransparent(bar.GetPixel(0, 0));
   g.DrawImage(bar);

As long as the Images are transparent (Which you can do at run-time with calls to Bitmap.MakeTransparent()), you can layer things with multiple calls to DrawImage without them "destroying" the bitmap.

只要图像是透明的(你可以在运行时调用Bitmap.MakeTransparent()),你就可以通过多次调用DrawImage来分层,而不会“破坏”位图。

#2


Sounds about right. I have a set of Texture2D xna objects, each of which stores its path to file. Using Image.FromFile() i use this path to create Image objects then I need to draw sections of the image, using Graphics.DrawImage() and a Rectangle describing the section, to a Graphics object. From here can I just make multiple g.DrawImage() calls in a Back to Front order.

听起来不错。我有一组Texture2D xna对象,每个对象都存储它的文件路径。使用Image.FromFile()我使用此路径创建Image对象然后我需要使用Graphics.DrawImage()和描述该部分的Rectangle绘制图像的部分到Graphics对象。从这里我可以按照Back to Front顺序进行多个g.DrawImage()调用。

Since Texture2D, and Image for that matter, can cope with transparency and my tex sources are all PNG format, I assume I can use the Image class instead of the Bitmap class and save having to the MakeTransparent call, correct?

由于Texture2D和Image就此而言,可以应对透明度,而我的tex源都是PNG格式,我假设我可以使用Image类而不是Bitmap类,并且不需要MakeTransparent调用,对吗?

#1


Is this what you're looking for?

这是你在找什么?

   Bitmap bmp = new Bitmap(100, 100);
   Graphics g = Graphics.FromImage(bmp);
   g.DrawImage(Properties.Resources.Foo);
   Bitmap bar = Properties.Resources.Bar;
   bar.MakeTransparent(bar.GetPixel(0, 0));
   g.DrawImage(bar);

As long as the Images are transparent (Which you can do at run-time with calls to Bitmap.MakeTransparent()), you can layer things with multiple calls to DrawImage without them "destroying" the bitmap.

只要图像是透明的(你可以在运行时调用Bitmap.MakeTransparent()),你就可以通过多次调用DrawImage来分层,而不会“破坏”位图。

#2


Sounds about right. I have a set of Texture2D xna objects, each of which stores its path to file. Using Image.FromFile() i use this path to create Image objects then I need to draw sections of the image, using Graphics.DrawImage() and a Rectangle describing the section, to a Graphics object. From here can I just make multiple g.DrawImage() calls in a Back to Front order.

听起来不错。我有一组Texture2D xna对象,每个对象都存储它的文件路径。使用Image.FromFile()我使用此路径创建Image对象然后我需要使用Graphics.DrawImage()和描述该部分的Rectangle绘制图像的部分到Graphics对象。从这里我可以按照Back to Front顺序进行多个g.DrawImage()调用。

Since Texture2D, and Image for that matter, can cope with transparency and my tex sources are all PNG format, I assume I can use the Image class instead of the Bitmap class and save having to the MakeTransparent call, correct?

由于Texture2D和Image就此而言,可以应对透明度,而我的tex源都是PNG格式,我假设我可以使用Image类而不是Bitmap类,并且不需要MakeTransparent调用,对吗?