简体   繁体   English

使用重叠控件在PictureBox中保存图像

[英]Save Image in PictureBox with overlapping Controls

I am developing an application in which user can select an image in a picture box. 我正在开发一个应用程序,用户可以在其中选择图片框中的图像。

After that he can right click on the image and add a user control which will again display an Image along with some text. 之后,他可以右键单击图像并添加一个用户控件,该控件将再次显示图像以及一些文本。 This user control can be added any number of times. 可以多次添加此用户控件。

User can also re-position the user controls as per need. 用户还可以根据需要重新定位用户控件。

All this functionality has been implemented and is working fine. 所有这些功能已经实现并且运行正常。

Now, the requirement is to save the Image along with the user control. 现在, 要求是将Image与用户控件一起保存。

在此输入图像描述

Above you can see the complete image which needs to be saved. 在上方您可以看到需要保存的完整图像。 Back image is the picture box image and the user control (small images with text). 背面图像是图片框图像和用户控件(带有文本的小图像)。

When user will click on save button the image should get saved on his disk as a single image. 当用户单击“保存”按钮时,图像应作为单个图像保存在其磁盘上。

This is a windows application developed in C#. 这是一个用C#开发的Windows应用程序

I want to know that whether this functionality can be achieved or not. 我想知道是否可以实现此功能。 If yes, then please guide me in the right direction. 如果是,那么请指导我正确的方向。

If you create a copy of the bitmap then with the Graphics.DrawImage() you can draw those images onto it. 如果您使用Graphics.DrawImage()创建位图的副本,则可以将这些图像绘制到其上。 You need to calculate the position of those controls. 您需要计算这些控件的位置。

Look here for DrawImage: http://msdn.microsoft.com/en-us/library/42807xh1.aspx 请看这里的DrawImage: http//msdn.microsoft.com/en-us/library/42807xh1.aspx

example: 例:

Bitmap copy = new Bitmap(OriginalBitmap);

Graphics g = Graphics.FromImage(copy);

g.DrawImage(arrowBitmap, new Point(..));

copy.Save(...);

A very simple and straight forward solution exists, has been thought of by Microsoft and includes these steps: Microsoft已经考虑过一个非常简单直接的解决方案,包括以下步骤:

  • Instead of PictureBox use a Panel and instead of using the Image property of the PictureBox use the BackgroundImage property of the Panel 而不是PictureBox使用Panel而不是使用PictureBoxImage属性使用PanelBackgroundImage属性

note: By using also the BackgroundImageLayout property you can quite easily instruct the Panel to stretch , center or zoom the image (I'm presuming the default value which is tile is not a good option in your case) 注意:通过使用BackgroundImageLayout属性,您可以非常轻松地指示Panel 拉伸居中缩放图像(我假设在您的情况下, tile的默认值不是一个好的选项)

  • Instead of placing the other user controls at higher Z order but alongside the previous PictureBox place them inside the Panel 而不是将其他用户控件放在更高的Z顺序,而是放在前面的PictureBox旁边,将它们放在Panel
  • Use the Control.DrawToBitmap method like so: 像这样使用Control.DrawToBitmap方法:

     private void button1_Click(object sender, EventArgs e) { var bmp = new Bitmap(this.panel1.Width, this.panel1.Height); this.panel1.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size)); bmp.Save(@"D:\\test.png", ImageFormat.Png); } 

That will result in your controls begin rendered along with the picture: 这将导致您的控件开始与图片一起呈现:

在此输入图像描述

Furthermore, and if your scenario allows it, you could simply use the DrawToBitmap method with any control which contains all of the actors you wish to render, for instance the actual Form . 此外,如果你的场景允许,你可以简单地使用DrawToBitmap方法和任何包含你想要渲染的所有actor的控件,例如实际的Form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM