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.
        }

画像ボックスに表示する画像を開いた後、コンボボックスから画像のサイズを選択できます44:

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.
}

ボタン 1 とコンボボックス 44 の両方のコーディングは、ユーザーがダイアログ ボックスから画像ファイルを選択できるようにするだけで、ボタン 1 とコンボボックス 44 はボタン 1 の画像を使用するという点でほぼ同じです。

しかし、コンパイル後にエラーが発生します。エラーは、この行「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 に答える