加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 用于大图像的Graphics.DrawImage替代品

发布时间:2020-12-16 01:56:16 所属栏目:百科 来源:网络整理
导读:我试图在图像上绘制带有反色的十字准线(“加号”),以显示图像中所选点的位置.我是这样做的: private static void DrawInvertedCrosshair(Graphics g,Image img,PointF location,float length,float width){ float halfLength = length / 2f; float halfWidt
我试图在图像上绘制带有反色的十字准线(“加号”),以显示图像中所选点的位置.我是这样做的:

private static void DrawInvertedCrosshair(Graphics g,Image img,PointF location,float length,float width)
{
    float halfLength = length / 2f;
    float halfWidth = width / 2f;

    Rectangle absHorizRect = Rectangle.Round(new RectangleF(location.X - halfLength,location.Y - halfWidth,length,width));
    Rectangle absVertRect = Rectangle.Round(new RectangleF(location.X - halfWidth,location.Y - halfLength,width,length));

    ImageAttributes attributes = new ImageAttributes();
    float[][] invertMatrix =
    { 
        new float[] {-1,0 },new float[] { 0,-1,1,new float[] { 1,1 }
    };
    ColorMatrix matrix = new ColorMatrix(invertMatrix);
    attributes.SetColorMatrix(matrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);

    g.DrawImage(img,absHorizRect,absHorizRect.X,absHorizRect.Y,absHorizRect.Width,absHorizRect.Height,GraphicsUnit.Pixel,attributes);
    g.DrawImage(img,absVertRect,absVertRect.X,absVertRect.Y,absVertRect.Width,absVertRect.Height,attributes);
}

它按预期工作,但它真的很慢.我希望用户能够使用鼠标移动选定的位置,方法是在移动时将位置设置为光标的位置.不幸的是,在我的计算机上,对于大图像,它每秒只能更新一次.

所以,我正在寻找使用Graphics.DrawImage来反转图像区域的替代方法.有没有办法以与所选区域区域而不是整个图像区域成比例的速度执行此操作?

解决方法

对我来说,你正在关注错误的问题.绘画图像很慢,不画“十字线”.

当你没有帮助时,大图像肯定会非常昂贵.而System.Drawing让人很容易找不到帮助.你想要做的两件基本事情是让图像画得更快,速度提高20倍以上是可以实现的:

>避免强制图像绘制代码重新缩放图像.而是只做一次,这样可以直接一对一地绘制图像而无需任何重新缩放.这样做的最佳时间是加载图像.可能再次出现在控件的Resize事件处理程序中.
>注意图像的像素格式.远射中最快的是像素格式,它与图像需要存储在视频适配器中的方式直接兼容.因此,图像数据可以直接复制到视频RAM,而无需调整每个像素.在99%的现代机器上,这种格式是PixelFormat.Format32bppPArgb.产生巨大的差异,它比其他所有产品快十倍.

一个简单的辅助方法,可以在不处理宽高比的情况下完成两者:

private static Bitmap Resample(Image img,Size size) {
    var bmp = new Bitmap(size.Width,size.Height,System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    using (var gr = Graphics.FromImage(bmp)) {
        gr.DrawImage(img,new Rectangle(Point.Empty,size));
    }
    return bmp;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读