如何在用户控件中创建PictureBox的实例 [英] how to create an instance of picturebox in user control

查看:54
本文介绍了如何在用户控件中创建PictureBox的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.我有一个用户控件,我想为同时加载的每张图片创建一个图片框实例.该过程在窗体加载后开始,窗体内的用户控件将出现.然后,用户控件将读取xml文件.它将从xml文件中另存为子节点的路径读取并加载所有图像.

我已经开始编写用户控件的代码:

Good Day. I have a user control and I want to create an instance of picture box for each picture that will load simultaneously. The process starts when the form did load, the user control inside the form will appear. Then the user control will read the xml file. It will read and load all the images from a path saved as child node in xml file.

I had started to code the user control:

private void UserControl1_Load(object sender, EventArgs e)
{
   XmlDocument xdoc = new XmlDocument();
   xdoc.Load(@"Items\XMLActiveX.xml");
   XmlNodeList xnl = xdoc.SelectSingleNode("imageroot").ChildNodes;

   label1.Text = String.Empty;

   foreach (XmlNode xNode in xnl) //for each node in xml file
   {
      XmlElement xElem = (XmlElement)xNode;
      XmlNodeList name = xElem.GetElementsByTagName("name");
      XmlNodeList x = xElem.GetElementsByTagName("x");
      XmlNodeList y = xElem.GetElementsByTagName("y");
      XmlNodeList path = xElem.GetElementsByTagName("path");

   for (int i = 0; i < name.Count; i++)
   {
                    
      label1.Text += "Image Name: " + name[i].InnerText + "\n";
      label1.Size += new System.Drawing.Size(100, 10);
      x1 = Convert.ToInt32(x[i].InnerText);
      y1 = Convert.ToInt32(y[i].InnerText);
      label1.Location = new Point(x1, y1);

    }
  }
}


我不知道如何创建多个图片框的实例以及如何加载图像.请帮我.任何帮助将不胜感激.提前致谢. :)


I don''t know how to create an instance of several picture box and how to load images. Please help me. Any help will be appreciated. Thanks in advance. :)

推荐答案



我使用快速创建的PictureBox-Controls为您编写了一个简单的示例.只需创建一个Windows Forms项目,然后将Program.cs替换为以下代码段即可.

Hi,

I wrote you a quick example using PictureBox-Controls created on the fly. Just Create a Windows Forms project and replace Program.cs with following snippet.

using System;
using System.Windows.Forms;
using System.Drawing;

namespace DynamicImages
{
    static class Program
    {
        static void Main()
        {
            Application.Run(new TestForm());
        }
    }
    class MyUserControl : Panel
    {
        public MyUserControl()
        {
            AutoScroll = true;
        }
        public void AddImagePictureBox(Image image)
        {
            PictureBox pb = new PictureBox();
            pb.Image = image;
            pb.Dock = DockStyle.Left;
            pb.SizeMode = PictureBoxSizeMode.AutoSize;
            Controls.Add(pb);
        }
    }
    class TestForm : Form
    {
        public TestForm()
        {
            MyUserControl control = new MyUserControl();
            control.Dock = DockStyle.Fill;
            Button buttonAddImage = new Button();
            buttonAddImage.Dock = DockStyle.Bottom;
            buttonAddImage.Text = "Add Image";
            buttonAddImage.Click += delegate(object obj, EventArgs ea)
            {
                // Create dummy image - replace with loading real image
                Random rand = new Random();
                Bitmap bmp = new Bitmap(rand.Next(100, 200), rand.Next(100, 200));
                using (Graphics grfx = Graphics.FromImage(bmp))
                    grfx.FillRectangle(new SolidBrush(Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))), 0, 0, bmp.Width, bmp.Height);
 
                control.AddImagePictureBox(bmp);
            };
            Controls.Add(control);
            Controls.Add(buttonAddImage);
        }
    }
}


但是还有另一种解决方案,如果您使用OwnerDraw控件,则可以在Paint事件处理程序中绘制图片(Graphics.DrawImage)(让我知道是否需要帮助


But there is another solution if you OwnerDraw your control, you can just draw the pictures (Graphics.DrawImage) inside the Paint event handler (let me know if you need help


我做了一些插入到您的代码中,以帮助您了解
你必须要做的.请参阅最后的说明.

I made some insertions into your code to help you get the idea of
what you have to do. See my explanation at the end.

private void UserControl1_Load(object sender, EventArgs e)
{
   XmlDocument xdoc = new XmlDocument();
   xdoc.Load(@"Items\XMLActiveX.xml"); //Dont hard code that into the control but rather make it a property of your user control
   XmlNodeList xnl = xdoc.SelectSingleNode("imageroot").ChildNodes;
   label1.Text = String.Empty;
   foreach (XmlNode xNode in xnl) //for each node in xml file
   {
      XmlElement xElem = (XmlElement)xNode;
      XmlNodeList name = xElem.GetElementsByTagName("name");
      XmlNodeList x = xElem.GetElementsByTagName("x");
      XmlNodeList y = xElem.GetElementsByTagName("y");
      XmlNodeList path = xElem.GetElementsByTagName("path");
      // Code saving x[] y[] path[] name[] is missing
      // ...
   }

   for (int i = 0; i < name.Count; i++)
   {
      //If you want to have a label for each picture you'd have to 
      //generate that too dynamically
      label1.Text += "Image Name: " + name[i].InnerText + "\n";
      label1.Size += new System.Drawing.Size(100, 10);
      x1 = Convert.ToInt32(x[i].InnerText);
      y1 = Convert.ToInt32(y[i].InnerText);
      label1.Location = new Point(x1, y1);
      PictureBox pb = new PictureBox();
      Image img = Image.FromFile(path[i]);
      pb.BackgroundImage = img;
      pb.Width = img.Width; //you can choose a fixed size too but then the picturebox would need to stretch or zoom
      // pb.BackgroundImageLayout = ImageLayout.Stretch;
      pb.Height = img.Height;
      //pb.Top = ???  You need to determine how you want to position
      //pb.Left= ???  you picturedox relatively to your label
      //For a different approach see my explanation in the answer
      // You also need to add the PictureBox to the control itself or
      // a container on your control
      // this.Controls.Add(pb);
    }
  }
}



如果要显示图像及其名称,建议您将其实现为单独的用户控件.它将使您的代码更加结构化.同样,将不同的值散布到单独的数组中也不是一个好主意.
创建一个小类,其中包含您要从XML中选取的数据,然后可以使用XmlSerialization提取数据.
您还需要考虑屏幕不动产以及如何最好地使用它.如果您无法控制或不知道可以得到多大图片,最好使用固定大小的PictureBox并将其设置为Stretch或Zoom.
如果需要将动态创建的控件添加到容器中.这可以是用户控件本身,也可以是用户控件上的容器,例如面板.
我没有使用PictureBox的Image属性来设置图像,而是使用BackgroundImage,因为它可以让您设置ImageLayout,以防需要自动拉伸或缩放.


问候,


曼弗雷德(Manfred)



If you want to display the image along with it''s name I would suggest that you implement that as a separate user control. It would make your code more structured. Also spreading of the different values into separate arrays is not a good idea.
Make a small class that contains the data you''re picking from the XML and you can then use XmlSerialization to extract the data.
You also need to think about screen realestate and how to best use it. If you cant control or know how big pictures can get it''s better to go with a fixed size PictureBox and set it to Stretch or Zoom.
If need to add dynamically created controls to a container. That can be your user control itself or a container you have on your user control like a panel for instance.
Instead of using PictureBox''s Image property to set the image I used BackgroundImage because it lets you set the ImageLayout in case you need to automatically stretch or zoom.


Regards,


Manfred


这里是狙击手.
Here is the snipper.
<br />
<pre><br />
Image bmp = Image.FromFile(@"C:\myAvatar.png");<br />
PictureBox box = new PictureBox() {<br />
 Image = bmp,<br />
};<br />
</pre><br />


这篇关于如何在用户控件中创建PictureBox的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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