关于打开要在winform中显示的位图文件的C#

时间:2012-10-04 07:25:48

标签: c# winforms

下面是我点击button1后编写打开要在图片框上显示的文件的编码方式:

        OpenFileDialog dlg = new OpenFileDialog();
        private void button1_Click(object sender, EventArgs e)
        {
            dlg.Filter = "Image (*bmp)|*.bmp|All Files|*.*";
            SetLogo = 0;

            if (dlg.ShowDialog() == DialogResult.OK)
            {

               this.pictureBox1.Image = (System.Drawing.Bitmap)Image.FromFile(dlg.FileName);

               int nMaxBitmapHeigth = 800;
               int nMaxBitmapWidth = 950;
                string strOrgFullPath = dlg.FileName;

                System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(strOrgFullPath);
                System.Drawing.Bitmap bmpOrg = null;

          // after this is the code for resizing the image to show on another picture box.
        }

打开要在图片框中显示的图像后,我可以从combobox44中选择图像的大小:

private void comboBox44_SelectedIndexChanged(object sender, EventArgs e)
{
    int nMaxBitmapHeigth = 800;
    int nMaxBitmapWidth = 950;

    System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(dlg.FileName);
    System.Drawing.Bitmap bmpOrg = null;

// after this is the code for resizing the image to show on another picture box.
}

button1和combobox44上的编码大致相同,只是button1允许用户从对话框中选择图像文件,而combobox44将使用button1中的图像。

但编译后出现错误。错误引用此行“System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(dlg.FileName);”在combobox44上。错误消息是“路径不是合法形式”。怎么了?为什么我不能使用button1中的图像?

2 个答案:

答案 0 :(得分:3)

直接从picturebox1拍摄图片,因为您已加载picurebox1

的图片,因此无需从文件中加载图片
 private void comboBox44_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nMaxBitmapHeigth = 800;
        int nMaxBitmapWidth = 950;

        if (this.pictureBox1.Image != null)
        {
            System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(this.pictureBox1.Image, new System.Drawing.Size(nMaxBitmapHeigth, nMaxBitmapWidth));
            System.Drawing.Bitmap bmpOrg = null;     
        }
        // after this is the code for resizing the image to show on another picture box.
    }

答案 1 :(得分:1)

我想更正代码如下:

    string strOrgFullPath = "";
    private void button1_Click(object sender, EventArgs e)
    {
        dlg.Filter = "Image (*bmp)|*.bmp|All Files|*.*";
        SetLogo = 0;

        if (dlg.ShowDialog() == DialogResult.OK)
        {

           this.pictureBox1.Image = (System.Drawing.Bitmap)Image.FromFile(dlg.FileName);

           int nMaxBitmapHeigth = 800;
           int nMaxBitmapWidth = 950;
            strOrgFullPath = dlg.FileName;

            System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(strOrgFullPath);
            System.Drawing.Bitmap bmpOrg = null;

      // after this is the code for resizing the image to show on another picture box.
    }


    private void comboBox44_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nMaxBitmapHeigth = 800;
        int nMaxBitmapWidth = 950;

        System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(strOrgFullPath);
        System.Drawing.Bitmap bmpOrg = null;

    // after this is the code for resizing the image to show on another picture box.
    }

   Try, Best of luck