如何使用C#将存储的图像从SQl服务器显示到ASP.net网页 [英] How to display the stored image from SQl server to ASP.net web page using C#

查看:99
本文介绍了如何使用C#将存储的图像从SQl服务器显示到ASP.net网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我是ASP.net的新手。在我的项目中,我想在网页上显示图像。但是这个条件是通过使用图像标签来完成的。但是我想从SQL服务器数据库中显示图像。我正在使用表格image_loads,下面将提到列。

表名:img_loads



img_recid INT

img_src IMAGE



如何从img_src列显示图像。



请给我建议如何解决它...

Hi
I am new to ASP.net. In my project i want to showing image on the web page.But this condition done by using image tag.But i want to show image from SQL server database.Here i'm using the table image_loads have the columns are mentioned below.
Table name:img_loads

img_recid INT
img_src IMAGE

How to show the image from img_src column.

Please give me advice how to solve it...

推荐答案

参考此链接:

图像显示1

图像显示2

Codeproject图像显示链接






根据我对你的问题的理解,你应该把img_src的数据类型变成varchar。

在此列中,您应该只保存图像的路径。和图像应该物理保存在项目目录中。



但是如果你不能改变img_sr列的数据类型并希望以字节的形式将图像存储在数据库中然后下面的文章将帮助你



http://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP。 Net.aspx

http://www.dotnetfox.com/articles/store-and-retrieve-image-from-database-in-Asp-Net-1113.aspx
Hi,

As per my understanding regarding your problem you should take data type of img_src is varchar.
In this column you should save only path of the image. and Image should be physically saved in your project directory.

But if you can not change the data type of img_sr column and want to store image in database in form of bytes then following article will help you

http://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx
http://www.dotnetfox.com/articles/store-and-retrieve-image-from-database-in-Asp-Net-1113.aspx


检查http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image(v=vs.110).aspx以获取Asp.Net中的图像控件/>




起初我以为你可以为控件分配一个位图图像,试着先找到它(我找不到它。 )



接下来是您需要解决的子问题。

1。从数据库中获取图像。

您需要将数据库值转换为bitmap [ ^ ]或图片 [ ^ ]对象。如果收到结果集,则包含该图像的对象是字节数组(byte [])。请查看 [ ^ ]等级。



2。一旦你有了一个图像,你可以将它分配给控件(如果你能找到那个选项)或者你可以将它写入临时文件夹并将uri提供给Image控件或编写HTML img标记。在后一种情况下,您可以直接将结果集对象转换为流并写入文件。第二种方法的问题是你需要找到一种方法来删除创建的资源,例如在会话结束时。



我会创建一个小原型并尝试一步逐步开始从结果集中获取字节数组并将其转换为流(写入文件并检入Windows资源管理器),将B转换为图像或位图对象。然后尝试将该图像转换为可以在网页上显示的控件。如果这不起作用,请写入文件并在网页中动态提供链接。第三,在会话结束事件中清理生成的文件。 (此时会话对象仍然是已知的,因此您可以将生成的文件名保留在会话对象中。在为多个用户处理多个文件时,请注意命名冲突...



希望这有帮助。
Check http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image(v=vs.110).aspx for the Image control in Asp.Net


At first I thought you could assign a bitmap image to the control, try to find that first (I couldn't find it immediately.)

Next are the subproblems you need to solve.
1. get the image from the database.
You'll need to get the database value into a bitmap [^]or Image [^]object. If you receive the resultset, the object containing the image is a byte array (byte []). Look at the Stream[^] class.

2. Once you have an image you can assign it to the control (if you can find that option) or alternatively you can write it to a temp folder and provide the uri to the Image control or write an HTML img tag. In the latter case you can just convert the resultset object directly to stream and write to file. The trouble with this second method is you need to find a way to also delete the created resources for example on session end.

I would create a small prototype and try step by step beginning by getting the byte array from the resultset and converting it A to a stream (write to file and check in windows explorer) and B to an Image or Bitmap object. Secondly try to convert that image to a control that can be shown on a web page. If that doesn't work, write to file and dynamically provide link in the web page. Thirdly, in the session end event cleanup the generated files. (The session object is still known at that point so you can keep the generated filenames in the session object. Beware of naming collisions when working with multiple files for multiple users...

Hope this helps.


这篇关于如何使用C#将存储的图像从SQl服务器显示到ASP.net网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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