C#MVC图片上传调整服务器端 [英] C# mvc image upload resizing server side

查看:106
本文介绍了C#MVC图片上传调整服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用,用户可以上传图片。我运行到目前的问题是,正在上传影像被保存到数据库中的原始格式。这将导致大量的性能问题,当图像在网页上使用。我用dotTrace分析的应用程序,我看到的图像时,从数据库处理显著的问题。

I've got an webapplication where users can upload images. The current problem i'm running into is that the images being uploaded are being saved to the database in the original format. This causes a lot of performance issues when the images are used on a webpage. I used dotTrace to profile the application and I see significant problems when images are processed from the database.

这个想法我已经是当它上传到服务器,以调整图像大小。看看下面的例子,我想申请当用户上传一个新的图像做;

The idea I have is to resize the image when it's uploaded to the server. Take the following example which I want the application to do when the user uploads a new image;


  1. 用户上传图片

  2. 图像被调整到7.500点¯x7.500像素的大小在72 dpi的

  3. 图像被保存到数据库

  4. 原始文件被处置

唯一保存的图像是上面提到的和web应用中包含的技术调整本上飞。

The only stored image is the one mentioned above and the webapplication contains technology to resize this on the fly.

我已经上所以这里读几个主题。且大多指向我到ImageMagick的方向。这个工具已经在我公司的熟悉,和PHP项目中使用。但是否有这个工具的良好和稳定的释放C#包装?我已经找到下面的工具,但他们无论是在Beta版本,alpha版本或目前没有更新。

I've already read several topics here on SO. And most of them point me into the direction of ImageMagick. This tool is already familiar at my company, and being used in PHP projects. But are there any good and stable released C# wrappers for this tool? I already found the tools below but they're either in Béta release, Alpha Release or currently not updated.

ImageMagick.NET

ImageMagick的APP

我也发现<一个href=\"http://stackoverflow.com/questions/1357257/image-resizing-efficiency-in-c-and-net-3-5\">this在SO话题。在这一主题下code例如供应;

I also found this topic on SO. In this topic the following code example is supplied;

private static Image CreateReducedImage(Image imgOrig, Size newSize)
{
    var newBm = new Bitmap(newSize.Width, newSize.Height);
    using (var newGrapics = Graphics.FromImage(newBm))
    {
        newGrapics.CompositingQuality = CompositingQuality.HighSpeed;
        newGrapics.SmoothingMode = SmoothingMode.HighSpeed;
        newGrapics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        newGrapics.DrawImage(imgOrig, new Rectangle(0, 0, newSize.Width, newSize.Height));
    }

    return newBm;
}

总之我有问题;

In short the questions i have;


  • 是否有使用上面的例子中就表现什么优势?

  • 是否有ImageMagick的一个良好的,可靠的C#包装我可以用它来做到这一点?

有关性能的任何其他好的建议,欢迎!

Any other good tips relating to the performance are welcome!

推荐答案

我们采用后一种方法 - 我不能评论的表现,但它肯定使操作更简单依赖

We use the latter approach - I can't comment on performance but it certainly makes handling dependencies simpler.

不过,有一点要注意的是,上面的code可能是太简单了,如果你的用户能够在各种格式上传图片。底层库(GDI +)具有的问题有很多的色彩格式,但它也依赖于OS版本。这里的code,我们使用的核心是:

However, one thing to note is that the above code is probably too simple if your users are able to upload images in all sorts of formats. The underlying library (GDI+) has issues with a lot of color formats, but it also is dependent on the OS version. Here's the core of the code we use:

    // GDI+ has problems with lots of image formats, and it also chokes on unknown ones (like CMYK).
// Therefore, we're going to take a whitelist approach.
// see http://bmpinroad.blogspot.com/2006/04/file-formats-pixel-formats.html
// also see http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c626a478-e5ef-4a5e-9a73-599b3b7a6ecc
PixelFormat format = originalImage.PixelFormat;

if (format == PixelFormat.Format16bppArgb1555 ||
    format == PixelFormat.Format64bppArgb)
{
    // try to preserve transparency
    format = PixelFormat.Format32bppArgb;
}
else if (format == PixelFormat.Format64bppPArgb)
{
    // try to preserve pre-multiplied transparency
    format = PixelFormat.Format32bppPArgb;
}
else if (format != PixelFormat.Format24bppRgb && format != PixelFormat.Format32bppRgb)
{
    format = PixelFormat.Format24bppRgb;
}


// GIF saving is probably still an issue.  If we ever need to tackle it, see the following:
// http://support.microsoft.com/kb/319061
// http://www.bobpowell.net/giftransparency.htm
// http://support.microsoft.com/kb/318343


using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, format))
{
    using (Graphics Canvas = Graphics.FromImage(newImage))
    {
        using (ImageAttributes attr = new ImageAttributes())
        {
            attr.SetWrapMode(WrapMode.TileFlipXY);

            Canvas.SmoothingMode = SmoothingMode.AntiAlias;
            Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Canvas.DrawImage(originalImage, new Rectangle(new Point(0, 0), newSize), srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, GraphicsUnit.Pixel, attr);
            newImage.Save(outputImageStream, originalImage.RawFormat);
        }
    }
}

这篇关于C#MVC图片上传调整服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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