我正在绘制矩形ROI区域,方法是使外部区域变暗,如下所示:

但是image.MakeTransparent需要太多的时间。提高绘图速度的最佳方法是什么?
void DrawRoi(Bitmap Image, RectangleF rect)
{
Rectangle roi = new Rectangle();
roi.X = (int)((float)Image.Width * rect.X);
roi.Y = (int)((float)Image.Height * rect.Y);
roi.Width = (int)((float)Image.Width * rect.Width);
roi.Height = (int)((float)Image.Height * rect.Height);
Stopwatch timer = new Stopwatch();
timer.Start();
// graphics manipulation takes about 240ms on 1080p image
using (Bitmap roiMaskImage = CreateRoiMaskImage(ImageWithRoi.Width, ImageWithRoi.Height, roi))
{
using (Graphics g = Graphics.FromImage(ImageWithRoi))
{
g.DrawImage(Image, 0, 0);
g.DrawImage(roiMaskImage, 0, 0);
Pen borderPen = CreateRoiBorderPen(ImageWithRoi);
g.DrawRectangle(borderPen, roi);
}
}
Debug.WriteLine("roi graphics: {0}ms", timer.ElapsedMilliseconds);
this.imagePictureBox.Image = ImageWithRoi;
}
Bitmap CreateRoiMaskImage(int width, int height, Rectangle roi)
{
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(image))
{
SolidBrush dimBrush = new SolidBrush(Color.FromArgb(64, 0, 0, 0));
g.FillRectangle(dimBrush, 0, 0, width, height);
SolidBrush roiBrush = new SolidBrush(Color.Red);
g.FillRectangle(roiBrush, roi);
image.MakeTransparent(Color.Red);
return image;
}
}
Pen CreateRoiBorderPen(Bitmap image)
{
float width = ((float)(image.Width + image.Height) * 2.5f) / (float)(640 + 480);
if (width < 1.0f)
width = 1.0f;
Pen pen = new Pen(Color.FromArgb(255, 0, 255, 0), width);
return pen;
}发布于 2011-09-03 19:46:44
嗯,你可以试着用25%的颜色将图像的外部变暗,而不是创建一个25%不透明的黑色区域,而是通过计算同一颜色25%暗的像素颜色值,而不需要创建一个新的32 but位图。
这可能会有帮助:StackOverflow确定RGB颜色亮度的公式。基于此,假设您选择Y = 0.2126 R + 0.7152 G + 0.0722 B作为您的亮度公式和0.75以及图像外部部分的新亮度--每个像素应该更改为:
Y = 0.75 * (0.2126 * R + 0.7152 * G + 0.0722 * B)
(R, G, B)' = (R - Y * 0.2126, G - Y * 0.7152, B - Y * 0.0722)(我两次意识到我放在这里的公式是错误的.这一次,我更加自信,但这可能是没有意义的,当我画,我还没有测试它。)
这将需要迭代中央广场外的每个单独的像素,所以不确定它最终是否会更快…但是,就像所有事物的性能一样,只有测量才能说明问题!
https://codereview.stackexchange.com/questions/4564
复制相似问题