GUID, the abbreviation of "Global Unique Identifier", is a unique reference number used as an identifier in computer software.
GUIDs are usually stored as 128-bit values, and are commonly displayed as 32 hexadecimal digits with groups separated by hyphens, such as:
- 21EC2020-3AEA-4069-A2DD-08002B30309D
My new task is to transfer the signature information, which directly saved as bytes[] data in DB. What I‘m gonna to do is to give every signature a unique name and save the file name in DB. GUID is perfect for my requirement.
Step1: Get the img bytes from DB
byte[] decodedImage = (byte[])reader["Signature"];
Step2: Transfer bytes to img
using (MemoryStream ms = new MemoryStream(decodedImage)) { Image img = Image.FromStream(ms); }
Step3: Give the img a unique name
using (MemoryStream ms = new MemoryStream(decodedImage)) { Image img = Image.FromStream(ms); var imgFileName = Guid.NewGuid().ToString() + ".png"; }
Step4: Save the file in system media folder and save the file name in DB
using (MemoryStream ms = new MemoryStream(decodedImage)) { Image img = Image.FromStream(ms); var imgFileName = Guid.NewGuid().ToString() + ".png"; fileName = imgFileName; idArr = reader["Id"].ToString(); string path = Path.GetFullPath(SIGNATURE_PATH); img.Save(path + imgFileName, System.Drawing.Imaging.ImageFormat.Png);}
string sql = "UPDATE [Kiwi-UAT].[dbo].[StaffClockSignature]" + "SET SignImageFile=‘" + fileName + "‘ WHERE Id=" + idArr; using(SqlCommand cmd = new SqlCommand(sql, conn)) { try { cmd.ExecuteNonQuery(); } catch (System.Exception e) { Console.WriteLine(e.Message); } }
- Tips: Using these package at the begining of your file
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.IO; using System.Drawing;
Now I‘m got the png file in my folder and the fileName record in DB table ^_^:
时间: 2024-10-06 05:20:25