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 and read files in binary form in .net

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to store and read binary files in. Net. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

The common storage and reading of images under. Net are as follows:

Store pictures: When storing pictures in binary form, set the fields in the database to Image data type (SQL Server), and store the data as Byte[].

1. Parameter is Image Path: Return Byte[] Type:

The code is as follows:

public byte[] GetPictureData(string imagepath)

{

////Open the file stream according to the path of the image file and save it as byte[]

FileStream fs = new FileStream(imagepath, FileMode.Open);//can be another overloaded method

byte[] byData = new byte[fs.Length];

fs.Read(byData, 0, byData.Length);

fs.Close();

return byData;

}

2. Parameter type is Image object, return Byte[] Type:

The code is as follows:

public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)

{

//Convert Image to stream data and save it as byte[]

MemoryStream mstream = new MemoryStream();

imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);

byte[] byData = new Byte[mstream.Length];

mstream.Position = 0;

mstream.Read(byData, 0, byData.Length);

mstream.Close();

return byData;

}

Well, this way you can convert the image into a Byte[] object through the above method, and then save this object to the database to save the binary format of the image to the database. I'm going to talk about how to read images from a database, which is actually the opposite process.

Read the picture: convert the corresponding field to Byte[], i.e. Byte[] bt=(Byte[])XXXX

1. Parameters are Byte[] type, return value is Image object:

The code is as follows:

public System.Drawing.Image ReturnPhoto(byte[] streamByte)

{

System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);

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

return img;

}

2. Parameter is Byte[] type, no return value, this is for ASP.NET image output from the page (Response.BinaryWrite)

The code is as follows:

public void WritePhoto(byte[] streamByte)

{

// Response. The default value for ContentType is "text/html"

Response.ContentType = "image/GIF";

The requested URL/image/GIF/JPEG was not found on this server.

Response.BinaryWrite(streamByte);

}

Additional:

For Response.ContentType values, there are other types besides the type for pictures:

The code is as follows:

Response.ContentType = "application/msword";

Response.ContentType = "application/x-shockwave-flash";

Response.ContentType = "application/vnd.ms-excel";

In addition, different output types can be used for different formats to suit different types:

The code is as follows:

switch (dataread("document_type"))

{

case "doc":

Response.ContentType = "application/msword";

case "swf":

Response.ContentType = "application/x-shockwave-flash";

case "xls":

Response.ContentType = "application/vnd.ms-excel";

case "gif":

Response.ContentType = "image/gif";

case "Jpg":

Response.ContentType = "image/jpeg";

}

Some related things can be used as a reference

The code is as follows:

Image image= GetImageFromKeyboard ();//Implement the ability to get images from the clipboard

System.IO.MemoryStream stream = new System.IO.MemoryStream();

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter

= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, image);

FileStream fs=new FileStream("xx",FileMode.Open,FileAccess.Write);

fs.Write(stream.ToArray(),0,stream.ToArray().Length);

Thank you for reading! About "how to store and read binary files in. Net" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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