asp.net of c implements the code for accessing binary images from sqlserver

  • 2020-05-12 02:29:18
  • OfStack

The following are the main implementation ideas:
1. Access to pictures
(1) convert the image file to base 2 and save it directly into sql server
 
//UploadHelper.cs 
/// <summary> 
///  Convert the image to long 2 Into the system  
/// </summary> 
/// <param name="photopath"></param> 
/// <returns></returns> 
public static Byte[] SetImgToByte(string imgPath) 
{ 
FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read); 
Byte[] byteData = new Byte[file.Length]; 
file.Read(byteData, 0, byteData.Length); 
file.Close(); 
return byteData; 
} 
/// <summary> 
///  Will be converted to 2 The base code image is saved to the database  
/// </summary> 
public static bool SaveEmployeeImg2Db(Employee model, string path) 
{ 
try 
{ 
Byte[] imgBytes = SetImgToByte(path); 
model.Photo = imgBytes; 
bool flag=EmployeeService.SaveEmployeePhoto(model); //EmployeeService It's an internal library call, insert or update photo for the company, no details here  
return flag; 
} 
catch (Exception ex) 
{ 
throw ex; 
} 
} 

(2) upload pictures in the web page
 
/// <summary> 
///  To upload pictures  
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
protected void btnUpload_Click(object sender, EventArgs e) 
{ 
string serverPath = Server.MapPath("~/images/"); 
if (this.fuPhoto.HasFile) //fuPhoto is fileupload controls  
{ 
string fileName = this.fuPhoto.PostedFile.FileName; 
FileInfo fi = new FileInfo(fileName); 
string mimeType = this.fuPhoto.PostedFile.ContentType.ToLower(); 
if (mimeType.IndexOf("image") < 0) 
{ 
//(" The photo uploaded is in the wrong format "); 
} 
else if(fi.Length > 2* 1024 * 1024) 
{ 
// Image is more than 2M , reprocessing  
} 
else 
{ 
string saveFilePath = serverPath + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName; 
try 
{ 
// Save the image to the server  
this.fuPhoto.PostedFile.SaveAs(saveFilePath); 
// into 2 Into the system  
Employee model = new Employee(int.Parse(id)); //id is EmployeeId Here is the mock field  
bool flag = UploadHelper.SaveEmployeeImg2Db(model, saveFilePath); 
} 
catch 
{ 
//(" Photo upload failed "); 
} 
finally 
{ 
// Finally, delete the image  
if (System.IO.File.Exists(saveFilePath)) 
{ 
System.IO.File.Delete(saveFilePath); 
} 
} 
} 
} 
else 
{ 
//(" Select all the photos you want to upload "); 
} 
} 

(3) fetch photos from the database (return format Image)
 
//UploadHelper.cs 
/// <summary> 
///  will 2 Convert the base to a picture Image 
/// </summary> 
/// <param name="photopath"></param> 
/// <returns></returns> 
public static System.Drawing.Image GetImgFromByte(Employee model) 
{ 
System.Drawing.Image img = null; 
try 
{ 
Stream stream = new MemoryStream(model.Photo); 
img = System.Drawing.Image.FromStream(stream,false); 
} 
catch 
{ 
img = null; 
} 
return img; 
} 

After the above method is taken out, if you are under winform, you can assign a value to the Image property of PictureBox. However, web does not have such a powerful control, so you have the following steps.
2, directly in the web page in the form of a stream to display pictures
(1) generate image stream page (ImgHelper.aspx)
The design page of this page has nothing. The class file is as follows:
 
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Collections.Generic; 
using System.IO; 
/// <summary> 
///  Picture auxiliary class  
/// </summary> 
public partial class ImgHelper : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!string.IsNullOrEmpty(Request["employeeId"])) // Employees who need to display photos on the page for delivery id 
{ 
int employeeId = int.Parse(Request["employeeId"]); 
Employee model = //EmployeeService.GetEmployeeByCondition(new Employee(employeeId))[0] as Employee; // Internal function   To find the 1 A staff   Don't give details  
try 
{ 
Byte[] byteImg = model.Photo; 
Stream stream = new MemoryStream(byteImg); 
System.Drawing.Bitmap img =(System.Drawing.Bitmap) System.Drawing.Bitmap.FromStream(stream, false); // Converted to Bitmap 
Response.Buffer = false; 
Response.ContentType = "image/jpg"; 
Response.AddHeader("Content-Disposition", "attachment;filename=photo.jpg");// The photo is called photo.jpg 
Response.BinaryWrite(byteImg);// write 2 Base flow  
Response.End(); 
} 
catch 
{ 
Response.End(); 
} 
} 
} 
} 

(2) the page that displays the photo calls ImgHelper.aspx
When the page loads, assign the image control the following values:
 
this.imgPhoto.ImageUrl = "/ImgHelper.aspx?employeeId="+tmpEmployee.Id.ToString(); //imgPhoto It's a picture control  

In general, 1 saves and 1 takes, which is very convenient for winform, but for webform, we need to have a little bit of a conversion idea. It would be nice if someone wrote a control like winform that directly binds to Image objects. The above code passed the test, I hope to help you.

Related articles: