Random rd = new Random();
string data = rd.Next(111111111, 999999999).ToString();
QRCodeGenerator q = new QRCodeGenerator();
QRCodeGenerator.QRCode qc = q.CreateQrCode(data,
QRCodeGenerator.ECCLevel.Q);
System.Drawing.Bitmap bm = qc.GetGraphic(28);
MemoryStream ms = new MemoryStream();
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] b = ms.ToArray();
Image1.ImageUrl = "data:image/png;base64," + `Convert.ToBase64String(b);`
String qr = Image1.ImageUrl.ToString();
我已生成qr代码并放入imagebox我需要将其保存到asp.net中的sql数据库
答案 0 :(得分:0)
我认为您不想在db中保存实际图像,只是链接到它:
Random rd = new Random();
string Path="D:\data\images\"; //folder where you want to store images
string data = rd.Next(111111111, 999999999).ToString();
QRCodeGenerator q = new QRCodeGenerator();
QRCodeGenerator.QRCode qc = q.CreateQrCode(data,
QRCodeGenerator.ECCLevel.Q);
System.Drawing.Bitmap bm = qc.GetGraphic(28);
Guid uniqueid=new Guid();
bm.Save(path+uniqueid.ToString()+'.png', System.Drawing.Imaging.ImageFormat.Png);
您只能在DB中保存此文件的链接 (此png文件的路径和文件名)以及要查询以获取此链接的标识符。
您需要在数据库中创建一个表,添加所需的字段 (一旦你这样做,我可以帮助你进一步在数据库中插入记录)
答案 1 :(得分:0)
首先在SQL中创建一个数据库,如下所示:
CREATE DATABASE QRDataBaseEntities
GO
CREATE TABLE Photo
(
PhotoID INT IDENTITY PRIMARY KEY,
Name NVARCHAR(100),
Photo VARBINARY(MAX)
)
然后在C#中创建一个函数,如下所示:
void SaveOnDatabase()
{
QRDataBaseEntities DbContext = new QRDataBaseEntities();
Photo Item = new Photo();
Item.Name = Image1;
Item.Photo=b;
DbContext.Photos.Add(Item);
DbContext.SaveChanges();
}