如何提高Crop方法的性能? [英] How to increase the performance to a Crop method?

查看:86
本文介绍了如何提高Crop方法的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好伙伴们,



我正在尝试为具有特定坐标的特定图像制作裁剪方法。在互联网上搜索时,我发现了一些例子。我找到的最快的是这个,我一路上做了一些修复。有人可以给我任何建议让它更快吗?



Hello fellows,

I am trying to make a crop method for a specific image with a specific coordinates. While searching around the Internet I found some examples. The fastest I found was this one and I made some fixes along the way. Can someone give me any suggestions to make it more faster?

private static Image CropImage(Image image,int xStart, int yStart, int height, int width)
        {
            try
            {
                if (image.Height < height)
                {
                    height = image.Height;
                }

                if (image.Width < width)
                {
                    width = image.Width;
                }

                var bmphoto = new Bitmap(width,height, PixelFormat.Format32bppPArgb);

                bmphoto.SetResolution(image.VerticalResolution, image.HorizontalResolution);

                var grPhoto = Graphics.FromImage(bmphoto);

                grPhoto.CompositingMode = CompositingMode.SourceOver;
                grPhoto.CompositingQuality = CompositingQuality.HighSpeed;
                grPhoto.InterpolationMode = InterpolationMode.Default;
                grPhoto.PixelOffsetMode = PixelOffsetMode.Half;
                grPhoto.SmoothingMode = SmoothingMode.None;
                
                grPhoto.DrawImage(image, new Rectangle(0, 0, width, height),xStart,yStart,width,height,GraphicsUnit.Pixel);

                var mm = new MemoryStream();

                bmphoto.Save(mm, ImageFormat.Jpeg);

                image.Dispose();
                bmphoto.Dispose();
                grPhoto.Dispose();

                var outimage = Image.FromStream(mm);

                return outimage;
            }
            catch (Exception ex)
            {
                throw new Exception("Error cropping image, the error was: " + ex.Message);
            }
        }

private void Button_Click(object sender, RoutedEventArgs e)
        {
            var stopwatch = new Stopwatch();

            var imagePath = @"11069.jpg";
            var resultImagePath = @" Woman Croped.jpg";

            stopwatch.Start();

            var resultImage = CropImage(new Bitmap(imagePath), 640,480,2400,1500); // ~177 ms
            resultImage.Save(resultImagePath);

            resultImage.Dispose();

            stopwatch.Stop();

            MessageBox.Show(stopwatch.Elapsed.Milliseconds + "ms");

        }





还有什么与CropImage()方法有关,以增加性能?

推荐答案

请参阅我对该问题的评论。根据我试图解释的原因,您的代码非常值得怀疑。我没有时间,但这种解决方案的性能也可能是一个问题。如果你需要纯净的作物,你不应该做任何可以导致更多一般效果的事情。你能在这里看到这一点吗?



所以,你应该更好地使用 System.Drawing.Bitmap.Clone,而不是做你正在做的事情。

http://msdn.microsoft.com/en-us/library/ms141944%28v=vs.110%29.aspx [ ^ ]。



请重新实施您的方法,计算时间并善于报告它如何影响性能。我打赌它会变得更好一些。你的计时技术非常好。



-SA
Please see my comment to the question. Your code is quite questionable, by the reasons I tried to explain. I did not time it, but performance of such solution can also be a concern. If you need a pure crop, you should not do anything which can cause much more general kinds of effects. Can you see the point here?

So, instead of doing what you are doing, you should better use System.Drawing.Bitmap.Clone:
http://msdn.microsoft.com/en-us/library/ms141944%28v=vs.110%29.aspx[^].

Please re-implement your method, time it and be so kind to report back how it effects the performance. I bet it should become somewhat better. Your timing technique is quite good.

—SA


这篇关于如何提高Crop方法的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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