Я не делаю ничего необычного, поэтому не уверен, есть ли какие-нибудь уловки, чтобы сделать это быстрее.
Это индикатор выполнения, на котором я рисую числовой текст сверху. Я рисую только изображение высотой в 1 пиксель, а затем меняю его размер. Но не уверен, есть ли способы получше.
Вот код:
void DrawProgressBar ( PictureBox pb, int ratio, bool reverse, params string [ ] percentages )
{
Color color1 = this.DarkRed;
Color color2 = this.DarkGreen;
if ( reverse )
{
color1 = this.DarkGreen;
color2 = this.DarkRed;
}
Bitmap bmp = new Bitmap ( 100, 1 );
using ( Graphics g = Graphics.FromImage ( bmp ) )
using ( SolidBrush brush = new SolidBrush ( color1 ) )
g.FillRectangle ( brush, 0, 0, 100, 1 );
for ( int i = 0 ; i < ratio ; ++i )
bmp.SetPixel ( i, 0, color2 );
Bitmap result = new Bitmap ( pb.Width + 2, pb.Height );
Graphics g2 = Graphics.FromImage ( result );
g2.InterpolationMode = InterpolationMode.NearestNeighbor;
g2.DrawImage ( bmp, 0, 0, result.Width, result.Height + 23 );
StringFormat strformat = new StringFormat ( );
strformat.Alignment = StringAlignment.Center;
strformat.LineAlignment = StringAlignment.Center;
SolidBrush br = new SolidBrush ( Color.White );
if ( percentages.Length > 1 )
{
decimal nratio = ratio / 100.0m;
int w = ( int ) ( result.Width * nratio );
Rectangle rect = new Rectangle ( 0, 0, w, result.Height );
Rectangle rect2 = new Rectangle ( w, 0, ( int ) ( result.Width - w ), result.Height );
if ( ratio > 5 )
g2.DrawString ( percentages [ 0 ], this.dataListView.Font, br, rect, strformat );
if ( 100 - ratio > 5 )
g2.DrawString ( percentages [ 1 ], this.dataListView.Font, br, rect2, strformat );
}
else
{
Rectangle rect = new Rectangle ( 0, 0, result.Width, result.Height );
g2.DrawString ( percentages [ 0 ], this.dataListView.Font, br, rect, strformat );
}
pb.Image = result;
}