需要一种获取图像并在占位符内返回图像的方法 [英] need a method that takes an image and return image within placeholder
本文介绍了需要一种获取图像并在占位符内返回图像的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要一种方法,可以给它一个图像,然后返回,它会给出放置在占位符中的相同图像,并在占位符上显示一个复选框..
thnx,
hi all ,
I need a method where I can give it an image and in returns it gives out the very same image placed within a placeholder with a check box displayed on the placeholder ..
thnx ,
推荐答案
听起来像您想要创建自己的自定义控件.您可能希望使用视觉设计器进行操作,但我想我将举一个示例说明您如何进行此操作.
首先是UserControl:
Sounds like you will want to create your own custom control. You will probably want to do the using the visual designer but I thought I would give an example of how you might go about doing this.
First the UserControl:
class CustomPictureBoxControl : UserControl
{
public CustomPictureBoxControl(Image img, int width, int height, Color backgroundColor)
{
//you could also use something like
//this.Width = img.Width;
//this.Height = img.Height + checkBox.Height;
this.Width = width;
this.Height = height;
this.Padding = new System.Windows.Forms.Padding(5);
this.BackColor = backgroundColor;
PictureBox picBox = new PictureBox();
//you could also pass the image path as a string
//i.e. CustomPictureBoxControl(string imgPath)
//in that case you would use picBox.ImageLocation = imgPath;
picBox.Image = img;
//or any of the other PictureBoxSizeModes
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
CheckBox checkBox = new CheckBox();
checkBox.Checked = true;
checkBox.Text = "Some text if any";
checkBox.Dock = DockStyle.Top;
picBox.Dock = DockStyle.Fill;
this.Controls.Add(checkBox);
this.Controls.Add(picBox);
}
}
以及如何使用它的示例:
And an example of how you could use it:
private void Form1_Load(object sender, EventArgs e)
{
Image img = (Image)new Bitmap("test2.png");
CustomPictureBoxControl customPicBox = CreateCustomPicBoxControl(img);
this.Controls.Add(customPicBox);
}
private CustomPictureBoxControl CreateCustomPicBoxControl(Image img)
{
CustomPictureBoxControl customPicBox = new CustomPictureBoxControl(img, 128, 128, Color.CornflowerBlue);
return customPicBox;
}
当然,您将需要对其进行修改.希望对您有所帮助.
Of course you will need to modify it to your needs. Hope this helps some.
这篇关于需要一种获取图像并在占位符内返回图像的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文