Let’s talk about the main implementation ideas:
1. Access pictures
(1) Convert the image file to binary and store it directly into SQL Server
//
/// <summary>
/// Convert the image to a long binary
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static Byte[] SetImgToByte(string imgPath)
{
FileStream file = new FileStream(imgPath, , );
Byte[] byteData = new Byte[];
(byteData, 0, );
();
return byteData;
}
/// <summary>
/// Save the image converted to binary code to the database
/// </summary>
public static bool SaveEmployeeImg2Db(Employee model, string path)
{
try
{
Byte[] imgBytes = SetImgToByte(path);
= imgBytes;
bool flag=(model); //EmployeeService is a library called, insert or update photos within the company. No details are disclosed here
return flag;
}
catch (Exception ex)
{
throw ex;
}
}
(2) Upload pictures on the web page
/// <summary>
/// Upload pictures
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
string serverPath = ("~/images/");
if () //fuPhoto is a fileupload control
{
string fileName = ;
FileInfo fi = new FileInfo(fileName);
string mimeType = ();
if (("image") < 0)
{
//("The uploaded photo format is incorrect");
}
else if( > 2* 1024 * 1024)
{
//The picture is greater than 2M, reprocess it
}
else
{
string saveFilePath = serverPath + ("yyyyMMddHHmmss") + fileName;
try
{
//Save the picture first to the server
(saveFilePath);
//Convert to binary
Employee model = new Employee((id)); //id is EmployeeId, here is the simulation field
bool flag = UploadHelper.SaveEmployeeImg2Db(model, saveFilePath);
}
catch
{
//("Photo upload failed");
}
finally
{
//Delete the picture in the end
if ((saveFilePath))
{
(saveFilePath);
}
}
}
}
else
{
//("Select all photos to upload");
}
}
(3) Take out photos from the database (return to format Image)
//
/// <summary>
/// Convert binary to image Image
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static GetImgFromByte(Employee model)
{
img = null;
try
{
Stream stream = new MemoryStream();
img = (stream,false);
}
catch
{
img = null;
}
return img;
}
After taking out the above method, if you are under winform, just assign a value to the Image attribute of a PictureBox. But there are no such powerful controls under the web, so the following steps are available.
2. Display pictures directly in the web page in the form of streams
(1) Generate image stream page (ImgHelper .aspx)
The design page of this page has nothing, the class file is as follows:
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
/// <summary>
/// Picture auxiliary category
/// </summary>
public partial class ImgHelper :
{
protected void Page_Load(object sender, EventArgs e)
{
if (!(Request["employeeId"])) //The employee id of the page that needs to be displayed to pass the photo.
{
int employeeId = (Request["employeeId"]);
Employee model = //(new Employee(employeeId))[0] as Employee; //Internal function Find an employee without revealing details
try
{
Byte[] byteImg = ;
Stream stream = new MemoryStream(byteImg);
img =() (stream, false); //Convert to Bitmap
= false;
= "image/jpg";
("Content-Disposition", "attachment;filename=");//The photo name is
(byteImg);//Write to binary stream
();
}
catch
{
();
}
}
}
}
(2) Call ImgHelper .aspx on the page that displays the photo
When the page is loading, assign values to the image control as follows:
= "/?employeeId="+(); //imgPhoto is a picture control
Overall, saving and withdrawing are very convenient for winform, but for webform, we need to have a slightly transformed idea. It would be better if someone wrote a control like Winform that directly binds Image objects. The above code has been tested and hope it will be helpful to you.
1. Access pictures
(1) Convert the image file to binary and store it directly into SQL Server
Copy the codeThe code is as follows:
//
/// <summary>
/// Convert the image to a long binary
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static Byte[] SetImgToByte(string imgPath)
{
FileStream file = new FileStream(imgPath, , );
Byte[] byteData = new Byte[];
(byteData, 0, );
();
return byteData;
}
/// <summary>
/// Save the image converted to binary code to the database
/// </summary>
public static bool SaveEmployeeImg2Db(Employee model, string path)
{
try
{
Byte[] imgBytes = SetImgToByte(path);
= imgBytes;
bool flag=(model); //EmployeeService is a library called, insert or update photos within the company. No details are disclosed here
return flag;
}
catch (Exception ex)
{
throw ex;
}
}
(2) Upload pictures on the web page
Copy the codeThe code is as follows:
/// <summary>
/// Upload pictures
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
string serverPath = ("~/images/");
if () //fuPhoto is a fileupload control
{
string fileName = ;
FileInfo fi = new FileInfo(fileName);
string mimeType = ();
if (("image") < 0)
{
//("The uploaded photo format is incorrect");
}
else if( > 2* 1024 * 1024)
{
//The picture is greater than 2M, reprocess it
}
else
{
string saveFilePath = serverPath + ("yyyyMMddHHmmss") + fileName;
try
{
//Save the picture first to the server
(saveFilePath);
//Convert to binary
Employee model = new Employee((id)); //id is EmployeeId, here is the simulation field
bool flag = UploadHelper.SaveEmployeeImg2Db(model, saveFilePath);
}
catch
{
//("Photo upload failed");
}
finally
{
//Delete the picture in the end
if ((saveFilePath))
{
(saveFilePath);
}
}
}
}
else
{
//("Select all photos to upload");
}
}
(3) Take out photos from the database (return to format Image)
Copy the codeThe code is as follows:
//
/// <summary>
/// Convert binary to image Image
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static GetImgFromByte(Employee model)
{
img = null;
try
{
Stream stream = new MemoryStream();
img = (stream,false);
}
catch
{
img = null;
}
return img;
}
After taking out the above method, if you are under winform, just assign a value to the Image attribute of a PictureBox. But there are no such powerful controls under the web, so the following steps are available.
2. Display pictures directly in the web page in the form of streams
(1) Generate image stream page (ImgHelper .aspx)
The design page of this page has nothing, the class file is as follows:
Copy the codeThe code is as follows:
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
/// <summary>
/// Picture auxiliary category
/// </summary>
public partial class ImgHelper :
{
protected void Page_Load(object sender, EventArgs e)
{
if (!(Request["employeeId"])) //The employee id of the page that needs to be displayed to pass the photo.
{
int employeeId = (Request["employeeId"]);
Employee model = //(new Employee(employeeId))[0] as Employee; //Internal function Find an employee without revealing details
try
{
Byte[] byteImg = ;
Stream stream = new MemoryStream(byteImg);
img =() (stream, false); //Convert to Bitmap
= false;
= "image/jpg";
("Content-Disposition", "attachment;filename=");//The photo name is
(byteImg);//Write to binary stream
();
}
catch
{
();
}
}
}
}
(2) Call ImgHelper .aspx on the page that displays the photo
When the page is loading, assign values to the image control as follows:
Copy the codeThe code is as follows:
= "/?employeeId="+(); //imgPhoto is a picture control
Overall, saving and withdrawing are very convenient for winform, but for webform, we need to have a slightly transformed idea. It would be better if someone wrote a control like Winform that directly binds Image objects. The above code has been tested and hope it will be helpful to you.