如何正确绘制图片框?

时间:2016-12-29 22:38:23

标签: c#

我在picturebox上的标准图形有问题。实际上我的图片框图像大小是x = ~5000 y = ~3000。如果我使用

Graphics gr = Picturebox1.Creategraphics();

滚动时一切都消失了。否则,如果我使用Graphics gr = Graphics.FromImage(Picturebox1.Image);

我总是需要重新加载图片,但这非常不舒服。我希望当用户“说”时,Picturebox图像会刷新。我发现了一些图形dll,但是它们不起作用。我怎样才能正确绘制上述内容?< / p>

1 个答案:

答案 0 :(得分:-1)

我猜你还没有找到油漆事件处理程序,所以就这样做:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    string fileLocation = "Get File Location";              //WHERE IS THE IMAGE LOCATED?  DON'T FORGET FILE EXTENTIONS!!

    //Graphics g = e.Graphics;                              //GRAPHICS INSTANCE
    Image img = Image.FromFile(fileLocation);               //IMAGE INSTANCE

    //  IF YOU PLACED A PICTUREBOX IN YOUR FORM.
    Size maxSize = new Size();                              //CREATE SIZE MAXIMUMS FOR THE LARGEST YOU WANT AN IMAGE TO BE
    Size imgSize = new Size();                              //FIND THE IMAGE SIZE FOR COMPARISON

    maxSize.Width = 600;                                    //SET MAX WIDTH
    maxSize.Height = 600;                                   //SET MAX HEIGHT
    imgSize.Width = img.Width;                              //FIND IMAGE WIDTH
    imgSize.Height = img.Height;                            //FIND IMAGE HEIGHT

    pbImage.MaximumSize = maxSize;                          //MAKE SURE WE DONT GO PAST OUR MAX SIZE
    pbImage.BackgroundImageLayout = ImageLayout.Stretch;    //MAKE SURE THE IMAGE STRETCHES TO THE SIZE OF THE PICTURE BOX


    //HERE, WE RUN A SERIES OS CHECKS TO SEE HOW BIG TO MAKE OUR PICTURE BOX
    if (imgSize.Height < maxSize.Height && imgSize.Width < maxSize.Width)           //IF THE PICTURE IS SMALLER THAN THE MAX SIZE
        pbImage.Size = imgSize;                                                     //SET THE SIZE TO THAT OF THE PICTURE
    else if (imgSize.Height > maxSize.Height || imgSize.Width > maxSize.Width)      //IF THE WIDTH OR HEIGHT ARE LARGER THAN THE MAX
    {
        //SET HEIGHT
        if (imgSize.Height < maxSize.Height)                                        
            pbImage.Height = imgSize.Height;
        else pbImage.Height = maxSize.Height;
        //SET WIDTH
        if (imgSize.Width < maxSize.Width)
            pbImage.Width = imgSize.Width;
        else pbImage.Width = maxSize.Width;
    }
    else if (imgSize.Height > maxSize.Height && imgSize.Width > maxSize.Width)      //IF THE IMAGE IS BIGGER THAN OUR MAX
        pbImage.Size = maxSize;                                                     //MAKE IT THE SIZE OF THE MAX

    pbImage.Image = img;                                                            //PUT THE IMAGE IN THE BOX


    //  IF YOU DIDN'T - YOU SHOULD.  IT'S FAR MORE CONTROLLED
    //g.DrawImage(img, 0, 0, (float)img.Width, (float)img.Height);

}

希望有所帮助。显然,修改string的{​​{1}}值以及您喜欢的最大宽度和高度。不管怎样,做更多的研究:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx https://msdn.microsoft.com/en-us/library/system.drawing.graphics(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.windows.controls.image.aspx