使用 jquery 显示 byte[] 图像

发布于 2024-12-09 15:06:53 字数 216 浏览 1 评论 0原文

当单击拇指图像时,我必须在同一视图的 div 中以实际大小显示图像。我将解释我的情况。我正在使用 asp.net mvc 2 应用程序。在我看来,我有图像的缩略图,这些图像作为 byte[] 存储在数据库中,并且 fileId 作为隐藏字段。单击缩略图时,我需要显示存储在数据库中的实际图像。实际图像应显示在同一视图中的 div 中,单击缩略图时应显示该视图。这应该使用 jquery 来实现。 感谢您对此进行调查。

I have to show the image in its actual size in a div in the same view when clicked on its thumb image. I will explain my scenario. I am using asp.net mvc 2 application. In my view I have thumb images of images which is stored in database as byte[] and also the fileId as hidden field. When clicked on the thumb image I need to show the actual image that is stored in the database.The actual image should be shown in a div in the same view which should be shown when click on thumb image. This should happen using jquery.
Thanks for looking into this.

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

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

发布评论

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

评论(2

无人问我粥可暖 2024-12-16 15:06:53

您可以使用 HttpHandler 来实现此目的。向您的项目添加一个通用处理程序,并在 ProcessRequest 方法中执行类似以下操作

            context.Response.Clear();

            var imageID = context.Request.QueryString["imageID"];   

            MemoryStream stream = new MemoryStream();   

            try
            {
                byte[] buffer = GetYourImageBuffer(imageID);                    

                if(buffer != null)
                {
                    stream = new MemoryStream(buffer);
                    Image image = Image.FromStream(stream);
                    context.Response.ContentType = "image/jpeg"; //Or what ever
                    image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                }                    
            }
            finally
            {
                context.Response.Flush();
                stream.Close();
            }

然后在您的 html 中您将能够通过以下方式调用它:

<img src="ImageHandler.ashx?imageID=<%: Model.Image.ID %>" />

You can use a HttpHandler for this. Add a generic handler to your project and do something like the following in the ProcessRequest method

            context.Response.Clear();

            var imageID = context.Request.QueryString["imageID"];   

            MemoryStream stream = new MemoryStream();   

            try
            {
                byte[] buffer = GetYourImageBuffer(imageID);                    

                if(buffer != null)
                {
                    stream = new MemoryStream(buffer);
                    Image image = Image.FromStream(stream);
                    context.Response.ContentType = "image/jpeg"; //Or what ever
                    image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                }                    
            }
            finally
            {
                context.Response.Flush();
                stream.Close();
            }

Then in your html you will be able to call this by...

<img src="ImageHandler.ashx?imageID=<%: Model.Image.ID %>" />
中性美 2024-12-16 15:06:53

如果数据库中的图像是字节格式的,那么首先需要将图像保存在服务器目录中,保存图像文件后,借助jquery将其显示。您还可以使用灯箱等插件以更具吸引力的方式显示图像。

这样,您将能够在使用 jquery 并发出 AJAX 请求来获取图像的同一视图上显示图像。

If you images in byte format in database, then you first need to save the image in a server directory and after saving the image file, display it with the help of jquery. You can also use plugins like lightbox to display the image in a more attractive way.

This way you will be able to show the image on the same view as you are using jquery and making an AJAX request to get the image.

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