Coolguy
Coolguy

Reputation: 2285

C# about opening a bitmap file to display in winform

Below is the way I write the coding to open a file to display on the picture box after I click 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.
        }

After open the image to display in the picture box, I can select the size of the image from the 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.
}

Both coding on button1 and combobox44 is about the same just that button1 enable the user to choose the image file from the dialog box and combobox44 will use back the image from button1.

But there's an error after compiling. The error refers to this line "System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(dlg.FileName);" on combobox44. The error message is "The path is not of a legal form." What's wrong? Why can't I use the image from button1?

Upvotes: 0

Views: 4473

Answers (2)

Anant Dabhi
Anant Dabhi

Reputation: 11144

Take a image directly from picturebox1 you don't need to load image from file because you already loaded image for 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.
    }

Upvotes: 3

Prasad
Prasad

Reputation: 154

I would like to correct the code as follows:

    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

Upvotes: 1

Related Questions