【发布时间】:2012-06-13 06:47:22
【问题描述】:
我有一个大小为 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 之前,一切看起来都很棒。它在最后创造了一个差距。我该如何解决这个问题,让它看起来仍然是全彩色的?
编辑:这是它的外观:
【问题讨论】:
-
为什么会有
height*2以及-1和18是从哪里来的?这样的事情值得评论。在实际代码中,尤其是在问题中。 -
我为两者都添加了 cmets。如果没有高度*2,它只会垂直填充一半的图片框。不知道为什么会这样。
标签: c# .net winforms drawing gdi