绘制图像的C#WinForms问题
类原始图像:表单 { 私有图像; 私人图片盒pb绘制图像的C#WinForms问题,c#,winforms,graphics,C#,Winforms,Graphics,类原始图像:表单 { 私有图像; 私人图片盒pb public OriginalImage() { pb = new PictureBox {SizeMode = PictureBoxSizeMode.CenterImage}; pb.SizeMode = PictureBoxSizeMode.StretchImage; Controls.Add(pb); image = Image.FromFile(@"Ima
public OriginalImage()
{
pb = new PictureBox {SizeMode = PictureBoxSizeMode.CenterImage};
pb.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pb);
image = Image.FromFile(@"Image/original.jpg");
this.Width = image.Width;
this.Height = image.Height;
this.Text = "Original image";
this.Paint += new PaintEventHandler(Drawer);
}
public virtual void Drawer(object source, PaintEventArgs e)
{
Graphics g = pb.CreateGraphics();
g.DrawImage(image,0,0);
}
我称之为按钮点击时以其他形式创建对象原始图像,但图像并没有绘制?问题在哪里
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var oi = new OriginalImage();
oi.Show();
}
}
您正在创建一个
PictureBox
并将其添加到控件中,但您从未实际使用过它(您在Paint
事件中手动绘制图像)。为什么?此控件可能会模糊窗体的绘图区域,因为在Paint
事件中,任何控件都位于您绘制的任何对象之上
此外,通过调用PictureBox
上的CreateGraphics
而不是表单本身,您将获得Graphics
对象。这是错误的,因为PictureBox
的绘制事件将在此代码之后触发,擦除您绘制的任何内容
我建议将您的原始格式更改为以下格式:
class OriginalImage: Form
{
private Image image;
private PictureBox pb;
public OriginalImage()
{
pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Dock = DockStyle.Fill; // this will make the PictureBox occupy the
// whole form
Controls.Add(pb);
image = Image.FromFile(@"Image/original.jpg");
this.ClientSize = new Size(image.Width, image.Height); // this allows you to
// size the form while
// accounting for the
// border
this.Text = "Original image";
pb.Image = image; // use this instead of drawing it yourself.
}
}
你为什么要从表单中实现呢?不仅仅是创建一个图像并将其添加到当前表单中?听起来他想用一个新的表单来显示图像。