如何在PictureBox C#中使用图像?

时间:2016-01-28 03:56:15

标签: c# winforms

我在PictureBox中设置的是:

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

但我收到的结果是pictureBox1.Image非常大。

如何在PictureBox中拟合图像?

我创建了gif动画来描述我的问题。

注意: 在这个gif图像中,pictureBox是我添加的自定义控件。 pictureBox1的父级是pictureBoxParent。我创建程序在主背景图像中插入文本(标签)或徽标(pictureBox)。

更新: 我在gif中添加了更多代码以显示。 此按钮创建一个新的PictureBox名称ctrl

public PictureBox ctrl = new PictureBox();

public void btnAddLogo_Click(object sender, EventArgs e)
{
    Random rnd = new Random();
    int randNumber = rnd.Next(1, 1000);
    String picName = "Pic_" + randNumber;
    //ctrl = new PictureBox();
    ctrl.Location = new Point(200, 170);
    ctrl.Size = new System.Drawing.Size(100, 60);
    ctrl.Name = picName;
    ctrl.SizeMode = PictureBoxSizeMode.Zoom;
    ctrl.Font = new System.Drawing.Font("NativePrinterFontA", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    ctrl.BackColor = Color.Chocolate;
    ctrl.MouseEnter += new EventHandler(control_MouseEnter);
    ctrl.MouseLeave += new EventHandler(control_MouseLeave);
    ctrl.MouseDown += new MouseEventHandler(control_MouseDown);
    ctrl.MouseMove += new MouseEventHandler(control_MouseMove);
    ctrl.MouseUp += new MouseEventHandler(control_MouseUp);
    picPreview.Controls.Add(ctrl);
    btnBackgroundImage.Enabled = true;
}

我已将ImageBackGround添加到ctrl PictureBox。

private void btnBackgroundImage_Click(object sender, EventArgs e)
{
    OpenFileDialog choofdlog = new OpenFileDialog();
    choofdlog.Filter = "All Files (*.*)|*.*";
    choofdlog.FilterIndex = 1;
    if (choofdlog.ShowDialog() == DialogResult.OK)
    {
        string sFileName = choofdlog.FileName;
        ctrl.Image = Image.FromFile(sFileName);
        ctrl.SizeMode = PictureBoxSizeMode.StretchImage;
    }
    else
    {
        return;
    }
}

在按钮中保存图像,我再次调用ctrl.SizeMode但不会发生。

ctrl.SizeMode = PictureBoxSizeMode.StretchImage;
g.DrawImage(ctrl.Image, Point.Round(StretchImageSize(postPoint, picPreview)));

Describe my problem when using SizeMode in PictureBox

0 个答案:

没有答案