Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 07:45:45 09月13日 Update

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!

Tags: Pictures data code files binaries arrays types databases methods content articles paths pages values examples interests just objects guys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB OPPO Reno Microsoft NVidia