显示图像时是否建议使用 .ashx ?

发布于 2024-12-14 23:13:18 字数 173 浏览 1 评论 0原文

是否建议使用处理程序来显示图像?问题是我无法右键单击并保存这些图像。它显示处理程序名称和 Asp.net 通用处理程序作为保存类型,因此我无法保存图像,但 GUI 图像的质量令人惊叹。我正在显示不同尺寸的单个图像使用 2 个处理程序并且代码工作正常,但如果我不使用处理程序,我必须单独调整它们的大小,然后将它们存储在不同的文件夹中。

Is it advisable to use a handler to display Images ? the issue is i cannot right click and save these images .It shows the Handler name and Asp.net Generic handler as save type so i cant save the image but the GUI image is amazing in quality.I am showing a single image in different sizes using 2 handlers and the code works fine but if then if I don't use the Handler i have to re size them individually and then store them in different folders.

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

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

发布评论

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

评论(2

最好是你 2024-12-21 23:13:18

我之前已经在网站上成功使用过这种方法,但只有在您的特定情况适合时才有用。如果您可以使用 HTML 来完成此操作,并且您的图像在服务器上的尺寸正确,那么它可能有点过大了。

如果您发现自己保存了同一图像的多个副本,那么您可能会受益于即时操作它。

在我的场景中,使用 .ashx 处理程序使我能够为每个主题上传一张大型高分辨率图像,然后将其转换为以多种不同方式使用:

  • 缩略图
  • 裁剪图像
  • “反射”效果(就像在闪亮表面上方一样)
  • 各种gallery

然后,只需从自定义图像控件调用它并传递正确的参数即可。

编辑:
关于保存图像,只要我强制浏览器尝试将其指定为 JPEG(或其他)的 .ashx 扩展名,我就可以在我的程序中做得很好。

我无法为您提供完整的 .ashx 示例,并且您需要考虑很多子主题:

  • 安全性 - 仅从某些位置读取
  • 格式 - 您是否要以 JPEG 等格式输出所有图像,与原始格式无关?
  • HttpHandlers - 如何创建和使用它们(不仅仅是图像)
  • 图形操作 - 熟悉 .NET
  • 内存流

中的 Graphics 对象作为一个小示例(在 VB.NET 中),这里有一个简单的方法在给定新的宽度和高度的情况下调整图像大小:

Imports System
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports CustomImaging
Imports System.Collections.Generic

Public Class ImgHandler : Implements IHttpHandler


  Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'Process the image and output it as a stream from memory

  End Sub

  Public Shared Function ResizeImage(ByVal image As System.Drawing.Image, ByVal width As Integer, ByVal height As Integer) As System.Drawing.Bitmap
        'a holder for the result
        Dim result As New Bitmap(width, height)
        'use a graphics object to draw the resized image into the bitmap
        Dim g As Graphics = Graphics.FromImage(result)
        Try
            'set the resize quality modes to high quality
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
            'draw the image into the target bitmap
            g.DrawImage(image, 0, 0, result.Width, result.Height)
        Catch ex As Exception

        Finally 'clean up resources
            If Not g Is Nothing Then g.Dispose()

        End Try
        'return the resulting image
        Return result
   End Function

   Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

I've successfully used this method on sites before, but it's only of use if your particular case is suitable. If you can do it using HTML and your images are the correct size on the server, then it's probably overkill.

If you find yourself saving several copies of the same image then you may benefit from manipulating it on the fly.

In my scenario, using a .ashx handler enabled me to upload just one large hi-res image per subject and then transform it for use in several different ways:

  • Thumbnail
  • Cropped image
  • 'Reflection' effects (like it was above a shiny surface)
  • Various galleries

Then it was just a case of calling it from a custom Image control and passing the correct parameters in.

EDIT :
Regarding saving the images, I can do it fine in mine as long as I force the .ashx extension that the browser tries to give it to be JPEG (or whatever).

I can't give you a full working .ashx example, and there are so many sub-topics you'll need to consider:

  • Security - only read from certain locations
  • Formats - are you going to output all images in, say, JPEG, regardles of the original format?
  • HttpHandlers - how to create and use them (not just for images)
  • Graphics manipulation - familiarise yourself with the Graphics object in .NET
  • Memory streams

As a small example (in VB.NET), here's a simple method to resize an image given the new width and height:

Imports System
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports CustomImaging
Imports System.Collections.Generic

Public Class ImgHandler : Implements IHttpHandler


  Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'Process the image and output it as a stream from memory

  End Sub

  Public Shared Function ResizeImage(ByVal image As System.Drawing.Image, ByVal width As Integer, ByVal height As Integer) As System.Drawing.Bitmap
        'a holder for the result
        Dim result As New Bitmap(width, height)
        'use a graphics object to draw the resized image into the bitmap
        Dim g As Graphics = Graphics.FromImage(result)
        Try
            'set the resize quality modes to high quality
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
            'draw the image into the target bitmap
            g.DrawImage(image, 0, 0, result.Width, result.Height)
        Catch ex As Exception

        Finally 'clean up resources
            If Not g Is Nothing Then g.Dispose()

        End Try
        'return the resulting image
        Return result
   End Function

   Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class
GRAY°灰色天空 2024-12-21 23:13:18

我不建议使用通用处理程序来显示图像。

我曾经使用这种方法,但除非图像经常变化,否则我认为这是浪费资源。

每次用户访问在 img 控件中包含图像的页面时,浏览器都必须向该文件发出 HTTP 请求,运行代码并返回流式图像。

例如,如果您只是出于图像缩略图的目的这样做,那么我建议每当在整个应用程序中上传图像时都保存缩略图的物理版本。

这样做的另一个优点是图像不会像代码那样出错!

I wouldn't recommend using generic handlers for displaying images.

I used to use this approach, but unless the image is changing quite often, I think it is a waste of resources.

Every time a user goes to a page which contains your image in an img control, the browser is having to make a HTTP Request to that file, run the code, and return a streamed image.

If you are only doing this for image thumbnail purposes, for example, I would recommend saving physical versions of the thumbnail image whenever the image is uploaded throughout the app.

Another advantage of this, is that images don't error as much as code!

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