Displaying an icon in a picturebox

后端 未结 3 1181
清酒与你
清酒与你 2020-12-17 19:21

I am trying to display icon file in a picture box. I\'m using this code to set the image.

pictureBox1.Image = new Icon(openFileDialog.FileName,          


        
相关标签:
3条回答
  • 2020-12-17 19:26

    Some icons are sized incorrectly 48x48 to 32x32.

    My final code is:

        Bitmap _image;
        try
        {
         _image = new Icon(icon, width, height).ToBitmap();
        }
        catch(ArgumentOutOfRangeException)
        {
         _image = Bitmap.FromHicon(new Icon(icon, new Size(width, height)).Handle);
        }
    
    0 讨论(0)
  • 2020-12-17 19:38

    Sometimes Bitmap.FromHicon doesn't convert perfectly. I find another solution:

    // event Paint of pictureBox1
    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawIcon(theIcon, 0, 0);
    }
    
    0 讨论(0)
  • 2020-12-17 19:47

    Solved the problem.

    pictureBox1.Image = Bitmap.FromHicon(new Icon(openFileDialog.FileName, new Size(48, 48)).Handle);
    
    0 讨论(0)
提交回复
热议问题