我通过将矩形区域外的区域变暗来绘制矩形区域,如此图像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;
}发布于 2011-09-05 16:29:22
根本不要操控图像。只需在您的图像顶部绘制“调光”即可。您可以实现相同的效果,例如,通过
1)在整个图像上绘制一个大的半透明区域,并将剪辑区域设置为您的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)绘制图像,然后绘制调光矩形,然后在其上绘制图像的roi部分。
3)绘制4个独立的矩形顶部/右侧/左侧/底部,以省略您的ROI。
发布于 2011-09-05 16:20:41
我完全看不出调用.MakeTransparent有什么意义。当您创建蒙版图像时
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);它已经是透明的了。最好将dimBrush绘制为四个独立的矩形(ROI上方/下方/左侧/右侧),避免在已经透明的区域中绘制!
发布于 2012-03-20 22:15:22
看一下WriteableBitmap类。在WritePixels方法中,你可以用一个Int32Rect定义一个感兴趣区域。
https://stackoverflow.com/questions/7292168
复制相似问题