在 ASP.NET 项目中调整用户上传图像的大小 – 图书馆?

发布于 2024-07-25 18:24:20 字数 128 浏览 3 评论 0原文

在一个相当简单的 ASP.NET 应用程序中,用户可以上传图像,大部分来自他的数码相机。 我必须将其大小调整为适合网络和缩略图的可用大小。 这里的最佳实践是什么? 有没有一个库可以让我以简单的方式实现,而无需在网络服务器上安装某些东西。

In a rather simple ASP.NET application where the user can upload a image, mostly from his digital camera. I have to resize it to a workable size for the web and a thumbnail.
What is here the best practice for? Is there a library that I in a simple way can implement without installing something on the web server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

纸短情长 2024-08-01 18:24:20

SO 上的这个线程可能会帮助您决定:

什么是最好的图像处理库< /a>

This thread on SO will probably help you decide:

What is the best image manipulation library

风吹雨成花 2024-08-01 18:24:20

免责声明:我是这个库的作者。

然而,它正是为此目的而设计的,并且非常成熟且经过充分测试。

http://nathanaeljones.com/products/asp-net-image-resizer/

我认为您会发现包含的示例应用程序正是您所需要的。

Disclaimer: I am the author of this library.

However, it was designed exactly for this purpose, and is very mature and well-tested.

http://nathanaeljones.com/products/asp-net-image-resizer/

I think you will find the included sample application does exactly what you need.

捂风挽笑 2024-08-01 18:24:20

看看 BitMap 类 - 你通过在构造函数中指定大小可以很容易地做到这一点。

因此,想象一下您想要将其减半:

Bitmap firstBitMap; // created somewhere else

// Create a new bitmap from the first, scaling down as we go
Bitmap halfSize = new Bitmap(firstBitMap, new Size(firstBitMap.Width/2, firstBitMap.Height/2));

如果您想要更高质量的解决方案,您需要 考虑 InterpolationMode 枚举

对于您描述的简单场景,您当然不需要费心使用第三方库。

Look at the BitMap class- you can do this quite easily by specifying the size in the constructor.

So imagine you wanted to half it:

Bitmap firstBitMap; // created somewhere else

// Create a new bitmap from the first, scaling down as we go
Bitmap halfSize = new Bitmap(firstBitMap, new Size(firstBitMap.Width/2, firstBitMap.Height/2));

If you want a higher quality solution, you need to consider the InterpolationMode enumeration.

For the simple scenario you describe you certainly don't need to bother with 3rd party libraries.

—━☆沉默づ 2024-08-01 18:24:20

我不是图像专家,但我在网站上实现了图像大小调整并使用了如下内容:

public static void ResizeImageHighQuality(string imgFilename, string imgResizedFilename, int resizeH, int resizeW)
{
    Image img = Image.FromFile(imgFilename);
    int h = 0, w = 0;

    if (img.Width > img.Height)
    {
        w = Convert.ToInt32(resizeW);
        h = Convert.ToInt32(w * Convert.ToDouble(img.Height) / Convert.ToDouble(img.Width));

    }
    else if (img.Height > img.Width)
    {
        h = Convert.ToInt32(resizeH);
        w = Convert.ToInt32(h * Convert.ToDouble(img.Width) / Convert.ToDouble(img.Height));
    }
    else
    {
        h = resizeH;
        w = resizeW;
    }

    Image thumbnail = new Bitmap(w, h);
    Graphics graphic = Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    graphic.DrawImage(img, 0, 0, w, h);

    ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
    EncoderParameters Params = new EncoderParameters(1);
    Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

    File.Delete(imgResizedFilename);
    FileStream fs = new FileStream(imgResizedFilename, FileMode.CreateNew);
    thumbnail.Save(fs, Info[1], Params);
    fs.Close();
}

I am no image expert, but I implemented image resizing on a website and used something like this:

public static void ResizeImageHighQuality(string imgFilename, string imgResizedFilename, int resizeH, int resizeW)
{
    Image img = Image.FromFile(imgFilename);
    int h = 0, w = 0;

    if (img.Width > img.Height)
    {
        w = Convert.ToInt32(resizeW);
        h = Convert.ToInt32(w * Convert.ToDouble(img.Height) / Convert.ToDouble(img.Width));

    }
    else if (img.Height > img.Width)
    {
        h = Convert.ToInt32(resizeH);
        w = Convert.ToInt32(h * Convert.ToDouble(img.Width) / Convert.ToDouble(img.Height));
    }
    else
    {
        h = resizeH;
        w = resizeW;
    }

    Image thumbnail = new Bitmap(w, h);
    Graphics graphic = Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    graphic.DrawImage(img, 0, 0, w, h);

    ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
    EncoderParameters Params = new EncoderParameters(1);
    Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

    File.Delete(imgResizedFilename);
    FileStream fs = new FileStream(imgResizedFilename, FileMode.CreateNew);
    thumbnail.Save(fs, Info[1], Params);
    fs.Close();
}
玩物 2024-08-01 18:24:20

使用 Bitmap(以及任何其他 System.Drawing 类)是 在 ASP.NET 中特别禁止(请参阅文档页面顶部附近的警告)。

使用它们可能会导致如下异常:

TypeInitializationException: The type initializer for 'System.Windows.Media.Brush' threw an exception.
   at System.Windows.Media.SolidColorBrush..ctor(Color color)
   ...
'System.Windows.Media.Transform' threw an exception.
   at System.Windows.Media.Brush..cctor()Win32Exception: The operation completed successfully
   at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at MS.Win32.MessageOnlyHwndWrapper..ctor()
   at System.Windows.Threading.Dispatcher..ctor()
   at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
   at System.Windows.Freezable..ctor()
   at System.Windows.Media.MatrixTransform..ctor(Matrix matrix)
   at System.Windows.Media.Transform..cctor()

并且

Win32Exception: The operation completed successfully
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext)
   at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher)

根据您尝试执行的操作,Windows 成像组件 可能会满足您的需求。 我们还很幸运地创建了一个 STA 线程并调用所有绘图操作。

Using Bitmap (and any other System.Drawing classes) is specifically prohibited in ASP.NET (see the warning near the top of the documentation page).

Using them may result in exceptions such as these:

TypeInitializationException: The type initializer for 'System.Windows.Media.Brush' threw an exception.
   at System.Windows.Media.SolidColorBrush..ctor(Color color)
   ...
'System.Windows.Media.Transform' threw an exception.
   at System.Windows.Media.Brush..cctor()Win32Exception: The operation completed successfully
   at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at MS.Win32.MessageOnlyHwndWrapper..ctor()
   at System.Windows.Threading.Dispatcher..ctor()
   at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
   at System.Windows.Freezable..ctor()
   at System.Windows.Media.MatrixTransform..ctor(Matrix matrix)
   at System.Windows.Media.Transform..cctor()

and

Win32Exception: The operation completed successfully
   at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
   at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext)
   at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher)

Depending on what you are trying to do, Windows Imaging Component may fill your need. We have also had luck by creating a single STA thread and invoking all drawing operations over to that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文