Reputation: 1838
I draw rectangular ROI region by darkening the area outside of it as in this image http://www.codeproject.com/script/Membership/Uploads/4613314/roi.jpg
But image.MakeTransparent
takes too much time. What is the best way to increase drawing speed?
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;
}
Upvotes: 0
Views: 3520
Reputation: 4013
Take a look at the WriteableBitmap class. In the WritePixels Method u can define a ROI with a Int32Rect.
Upvotes: 0
Reputation: 11111
Don't manipulate the image at all. Just draw the "dimming" on top of your image. You can achieve the same effect for example by
1) Drawing a large semi-transparent region over the entire image, with the clip region set to your ROI
// 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) Drawing the image, then the dimming rectangle, then the roi part of the image on top of that.
3) Drawing 4 separate rectangles top/right/left/bottom to leave out your ROI.
Upvotes: 2
Reputation: 9244
I can't see any point in calling .MakeTransparent at all. When you create your mask image
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
it is already transparent. It would be better to draw the dimBrush as four separate rectangles (areas above/below/left of/right of ROI) instead and avoid drawing in the area that already is transparent!
Upvotes: 1