0

以下是我编写代码以在单击 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);” 在组合框 44 上。错误消息是“路径不是合法形式”。怎么了?为什么我不能使用 button1 中的图像?

4

2 回答 2

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.
    }
于 2012-10-04T07:49:17.793 回答
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
于 2012-10-04T07:34:32.960 回答