加快图像处理 [英] Speeding Up Image Handling

查看:97
本文介绍了加快图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题如何处理的后续操作图像作为CAD应用程序的背景

我应用了大小调整/重采样代码,但没有任何区别.我敢肯定我对GDI +等的了解不多,所以如果我感到困惑,请原谅.

我正在使用第三方图形库(Piccolo).除了均匀地包装GDI +外,我还不知道它到底是做什么的.

我的测试是在不同的缩放级别旋转显示-这是导致性能最差的过程.我知道我正在旋转相机视图.在高达1.0的缩放级别下,不会降低性能,并且使用鼠标滚轮可以平滑旋转.必须以1.0的缩放比例将图像缩放为每像素1m的CAD单位.我已经调整了图像的大小/重新采样以匹配该图像.我根据上一个问题给出的代码尝试了不同的方法来加快此速度:

public static Bitmap ResampleImage(Image img, Size size) {
            using (logger.VerboseCall()) {

                var bmp = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppPArgb);
                using (var gr = Graphics.FromImage(bmp)) {
                    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;                 

                    gr.DrawImage(img, new Rectangle(Point.Empty, size));
                }
                return bmp;
            }
        }

我猜想这可以加快重采样的速度,但是据我所知,在尝试以高缩放级别旋转显示器时,对性能没有任何影响.使用性能分析器(ANTS),我能够找到导致性能下降的代码:

 protected override void Paint(PPaintContext paintContext) {
            using (PUtil.logger.DebugCall()) {
                try {
                    if (Image != null) {
                        RectangleF b = Bounds;

                        Graphics g = paintContext.Graphics;

                        g.DrawImage(image, b);
                    }
                }
                catch (Exception ex) {
                    PUtil.logger.Error(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
                    //----catch GDI OOM exceptions
                }
            }
        }

性能下降完全在g.DrawImage(image,b);

界限当然是图像的界限. catch块在那里可以捕获GDI + OOM异常,在高缩放级别下,这种异常似乎也更糟.

此调用的次数似乎随着缩放级别的增加而增加....

在绘制摄像机视图的代码中还有另一个问题,但是我没有足够的信息来说明这一点,只是这似乎会绘制摄像机上附着的所有图层-我假设它们上的所有对象-当何时相机的视图矩阵和剪辑将应用到paintContext(无论如何).

所以还有其他对g.DrawImage(image,b)的调用;我可以用?还是我受制于图形引擎?不幸的是,它是如此嵌入,以至于我很难改变

再次感谢

解决方案

我认为您使用的是Piccolo的 PImageNode 对象.该方法的调用数量可能会增加,因为Piccolo引擎会基于缩放级别(剔除的种类)在用户屏幕上跟踪真实"绘图区域,并仅绘制可见的节点.如果场景中有很多 PImageNode 对象并制作 ZoomOut ,则会增加需要绘制 PImageNode 对象的数量,因此对该方法的调用.

性能如何:

1)尝试使用SetStyle(ControlStyles.DoubleBuffer,true); PCanvas的数量(如果尚未设置)

2)看这里 CodeProject

致谢.

This is a follow on to my question How To Handle Image as Background to CAD Application

I applied the resizing/resampling code but it is not making any difference. I am sure I do not know enough about GDI+ etc so please excuse me if I seem muddled.

I am using a third party graphics library (Piccolo). I do not know enough to be sure what it is doing under the hood other than it evenually wraps GDI+.

My test is to rotate the display at different zoom levels - this is the process that causes the worst performance hit. I know I am rotating the camera view. At zoom levels up to 1.0 there is no performance degradation and rotation is smooth using the mouse wheel. The image has to be scaled to the CAD units of 1m per pixel at a zoom level of 1.0. I have resized/ resampled the image to match that. I have tried different ways to speed this up based on the code given me in the last question:

public static Bitmap ResampleImage(Image img, Size size) {
            using (logger.VerboseCall()) {

                var bmp = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppPArgb);
                using (var gr = Graphics.FromImage(bmp)) {
                    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;                 

                    gr.DrawImage(img, new Rectangle(Point.Empty, size));
                }
                return bmp;
            }
        }

I guess this speeds up the resample but as far as I can tell has no effect on the performance when trying to rotate the display at high zoom levels. User a performance profiler (ANTS) I am able to find the code that is causing the performance hit:

 protected override void Paint(PPaintContext paintContext) {
            using (PUtil.logger.DebugCall()) {
                try {
                    if (Image != null) {
                        RectangleF b = Bounds;

                        Graphics g = paintContext.Graphics;

                        g.DrawImage(image, b);
                    }
                }
                catch (Exception ex) {
                    PUtil.logger.Error(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
                    //----catch GDI OOM exceptions
                }
            }
        }

The performance hit is entirely in g.DrawImage(image, b);

Bounds is the bounds of the image of course. The catch block is there to catch GDI+ OOM exceptions which seem worse at high zoom levels also.

The number of times this is called seems to increase as the zoom level increases....

There is another hit in the code painting the camera view but I have not enough information to explain that yet except that this seems to paint all the layers attached to the camera - and all the objects on them I assume - when when the cameras view matrix and clip are applied to the paintContext (whatever that means).

So is there some other call to g.DrawImage(image, b); that I could use? Or am I at the mercy of the graphics engine? Unfortunately it is so embedded that it would be very hard to change for me

Thanks again

解决方案

I think you you use,if I'm not mistake, PImageNode object form Piccolo. The quantity of calls to that method could increase because Piccolo engine traces "real" drawing area on the user screen, based on zoom level (kind of Culling) and draws only the nodes which are Visible ones. If you have a lot of PImageNode objects on your scene and make ZoomOut it will increase the quantity of PImageNode objects need to be drawn, so the calls to that method.

What about the performance:

1) Try to use SetStyle(ControlStyles.DoubleBuffer,true); of the PCanvas (if it's not yet setted up)

2) look here CodeProject

Regards.

这篇关于加快图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆