简体   繁体   中英

Why is my bitmap scaling changes when I set the PictureBox's BorderStyle to FixedSingle?

I have a PictureBox with size 1000, 20, and I set it's bitmap like so on Form_Load event:

void Form1_Load ( object sender, EventArgs e )
{
    SetProgressBar ( pictureBox1, 25 );
}

void SetProgressBar ( PictureBox pb, int progress )
{
    Bitmap bmp = new Bitmap ( 100, 1 );

    using ( Graphics gfx = Graphics.FromImage ( bmp ) )
    using ( SolidBrush brush = new SolidBrush ( System.Drawing.SystemColors.ButtonShadow ) )
    {
        gfx.FillRectangle ( brush, 0, 0, 100, 1 );
    }

    for ( int i = 0; i < progress; ++i )
    {
        bmp.SetPixel ( i, 0, Color.DeepSkyBlue );
    }

    // If I don't set the resize bitmap size to height * 2, then it doesn't fill the whole image for some reason.
    pb.Image = ResizeBitmap ( bmp, pb.Width, pb.Height * 2, progress );
}

public Bitmap ResizeBitmap ( Bitmap b, int nWidth, int nHeight, int progress )
{
    Bitmap result = new Bitmap ( nWidth, nHeight );

    StringFormat sf = new StringFormat ( );
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
    Font font = new Font ( FontFamily.GenericSansSerif, 10, FontStyle.Regular );

    using ( Graphics g = Graphics.FromImage ( ( Image ) result ) )
    {
            // 20 is the height of the picturebox
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        g.DrawImage ( b, 0, 0, nWidth, nHeight );
        g.DrawString ( progress.ToString ( ), font, Brushes.White, new RectangleF ( 0, -1, nWidth, 20 ), sf );
    }
    return result;
}

Everything looks great until I set the BorderStyle to FlatSingle. It creates a gap at the end. How can I fix this so it still looks fully colored all the way?

EDIT: This is how it looks:

在此输入图像描述

It appears that adding the borders in increasing the size of your pictureboxes. I set up your example with BorderStyle set to FixedSingle,None and Fixed3D the results were different for each one. Each of these PictureBoxes are starting at the same location as you can see the visible width has actually changed.

在此输入图像描述

It worked out to be about 4 pixels pn the FixedSingle and 2 pixels on the Fixed3D. I am not sure what is going on under the hood but you should be able to code around it like this.

 if (pb.BorderStyle == BorderStyle.FixedSingle)
     pb.Image = ResizeBitmap(bmp, pb.Width + 4, pb.Height * 2, progress);
 else if (pb.BorderStyle == BorderStyle.Fixed3D)
     pb.Image = ResizeBitmap(bmp, pb.Width + 2, pb.Height * 2, progress);
 else 
     pb.Image = ResizeBitmap(bmp, pb.Width, pb.Height * 2, progress);

Which gives a result that looks like this.

在此输入图像描述


If you look at the DisplayRectangle Property you will see that your client rectangle is changing when you change your BorderStyle:

BorderStyle.None = 1000 pixels
BorderStyle.Fixed3D = 996 pixels
BorderStyle.FixedSingle = 998 pixels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM