我通过使其外部区域变暗来绘制矩形ROI区域,如图http://www.codeproject.com/script/Membership/Uploads/4613314/roi.jpg
但是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;
}
答案 0 :(得分:2)
根本不操纵图像。只需在图像上绘制“调光”即可。您可以通过
实现相同的效果1)在整个图像上绘制一个大的半透明区域,剪辑区域设置为 您的投资回报率
// Assume for simplicity the image is size w*h, and the graphics is the same size.
// The ROI is a rectangle named roiRect.
g.DrawImageUnscaled(image, 0, 0 , w, h);
g.SetClip(roiRect);
g.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Black)), 0, 0, w, h);
g.ResetClip();
2)绘制图像,然后绘制变暗矩形,然后绘制图像的roi部分。
3)在顶部/右侧/左侧/底部绘制4个单独的矩形,以省去您的投资回报率。
答案 1 :(得分:1)
我无法看到调用.MakeTransparent的任何一点。创建蒙版图像时
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
它已经透明了。最好将dimBrush绘制为四个单独的矩形(ROI上/下/左边的区域),避免在已经透明的区域中绘制!
答案 2 :(得分:0)
看看WriteableBitmap类。在WritePixels方法中,您可以使用Int32Rect定义ROI。