我有一个大小为 1000、20 的 PictureBox,我在 Form_Load 事件中设置它的位图:
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;
}
在我将 BorderStyle 设置为 FlatSingle 之前,一切看起来都很棒。它在最后创造了一个差距。我该如何解决这个问题,让它看起来仍然是全彩色的?
编辑:这就是它的外观: