在picturebox中打开的图像不可编辑

时间:2014-01-01 12:16:44

标签: c# visual-studio-2010 picturebox windows-forms-designer

我正在编写类似于MS Paint的程序。我正在尝试使用OpenFileDirectory打开.jpg文件。代码如下:

ofd.Reset();
ofd.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png";
ofd.InitialDirectory = "c:\\Users\\Icek\\Desktop";
ofd.Title = "Open";
ofd.Multiselect = false;

if (ofd.ShowDialog() == DialogResult.OK)
{
   var.Image = Image.FromFile(ofd.FileName);
}

图片正常加载,但我不能再画了。谁能告诉我它为什么会发生?

1 个答案:

答案 0 :(得分:0)

您必须从图片框

创建图形参考

如何:在控件上绘制文本

private void DrawString()
{
    System.Drawing.Graphics pic = pictureBox1.CreateGraphics();
    string drawString = "Sample Text";
    System.Drawing.Font drawFont = new System.Drawing.Font(
        "Arial", 16);
    System.Drawing.SolidBrush drawBrush = new
        System.Drawing.SolidBrush(System.Drawing.Color.Black);
    float x = 150.0f;
    float y = 50.0f;
    pic.DrawString(drawString, drawFont, drawBrush, x, y);
    drawFont.Dispose();
    drawBrush.Dispose();
    pic.Dispose();
}

private void button1_Click(object sender, EventArgs e)
{
    DrawString();
}

最后如果你得到图形上下文,那么可以使用DrawLine,Curve,Image ......

有关详细信息,请参阅System.Drawing.Graphics