Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to store pictures in database and read pictures by Asp.net

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how Asp.net stores pictures in the database and reads pictures. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

There is a lot of information about uploading pictures to the database by ASP.NET, which is commonly used as follows:

There are several ways to store image type data:

1. Convert a picture to a binary array (byte [])

The code is as follows:

Byte [] fileData = this.FileUpload1.FileBytes

two。 Convert a file to a binary array based on the path

The code is as follows:

Code

Public byte [] returnbyte (string strpath)

{

/ / read the file in binary mode

FileStream fsMyfile = new FileStream (strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite)

/ / create a binary data stream reader that is associated with an open file

BinaryReader brMyfile = new BinaryReader (fsMyfile)

/ / relocate the file pointer to the beginning of the file

BrMyfile.BaseStream.Seek (0, SeekOrigin.Begin)

Byte [] bytes = brMyfile.ReadBytes (Convert.ToInt32 (fsMyfile.Length.ToString ()

/ / close each object of the above new

BrMyfile.Close ()

Return bytes

}

The 3img type gets a binary array

The code is as follows:

Public static byte [] Getbyte (Image img)

{

MemoryStream stream = new MemoryStream ()

Img.Save (stream, ImageFormat.Jpeg)

Byte [] mydata = new byte [stream.Length]

Mydata = stream.ToArray ()

Stream.Close ()

Return mydata

}

Data of type image is read and displayed on a web page as follows:

one. Return the image type directly

The code is as follows:

Private System.Drawing.Image getImageDataFromOracle ()

{

String sql = "select IMGDATA from t_img where imgID=100"

String strconn = System.Configuration.ConfigurationManager.ConnectionStrings ["ConnectionStringForOracle"] .ToString ()

OracleConnection oraConn = new OracleConnection (strconn)

OracleCommand oraComm = new OracleCommand (sql, oraConn)

OraConn.Open ()

Byte [] fileData = (byte []) oraComm.ExecuteScalar ()

OraConn.Close ()

System.IO.MemoryStream ms = new System.IO.MemoryStream (fileData)

System.Drawing.Image img = System.Drawing.Image.FromStream (ms)

Return img

}

two。 Use page input to display pictures

Page ImageShow.aspx (Page_Load method)

The code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

Byte [] b_logoImg = (byte []) dt_channelImg.Rows [0] ["LogoImage"]; / / get the byte [] array. This is just an example.

If (b_logoImg.Length > 0)

{

System.Drawing.Image logoImg

MemoryStream ms = new MemoryStream (b_logoImg)

Response.Clear ()

Response.ContentType = "image/gif"

Response.OutputStream.Write (b_logoImg, 0, b_logoImg.Length)

Response.End ()

}

}

The image path is written as follows:

The above is all the contents of the article "how to store pictures in the database and read pictures by Asp.net". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report