C# 4.0 扫描后如何将图像保存到数据库
我开发了一个windows应用程序来扫描图像。扫描图像后,我想直接将其保存到数据库中,而不是本地计算机中…我使用的代码如下C# 4.0 扫描后如何将图像保存到数据库,c#-4.0,C# 4.0,我开发了一个windows应用程序来扫描图像。扫描图像后,我想直接将其保存到数据库中,而不是本地计算机中…我使用的代码如下 try { String str = string.Empty; WIA.CommonDialogClass scanner; ImageFile imageObject; scanner = new CommonDialogClass(); imageObject = scanner.ShowAcquireImage
try
{
String str = string.Empty;
WIA.CommonDialogClass scanner;
ImageFile imageObject;
scanner = new CommonDialogClass();
imageObject = scanner.ShowAcquireImage
(WiaDeviceType.ScannerDeviceType,
WiaImageIntent.ColorIntent,
WiaImageBias.MinimizeSize,
ImageFormat.Jpeg.Guid.ToString("B"),
false,
true,
true);
str = DateTime.Now.ToString();
str = str.Replace("/", "");
str = str.Replace(":", "");
Directory.CreateDirectory("D:\\scanned1");
// MessageBox.Show(string.Format("File Extension = {0}\n
//\nFormat = {1}", imageObject.FileExtension, imageObject.FormatID));
imageObject.SaveFile(@"D:\scanned1\lel" + str + ".jpg");
MessageBox.Show("Scanning Done");
}
catch (Exception ex)
{
MessageBox.Show("Please check if the scanner is connected properly.");
}
我不想将其保存到D驱动器,而是想将其保存到数据库……我该怎么做?请回答……我不知道“ImageFile”到底是什么,但应该有一种方法将其转换为字节数组(byte[])。之后,需要将该数组插入sql中的varbinary字段。
大概是这样的:
using(SqlCommand cmd = new SqlCommand("INSERT INTO MyTable (myvarbinarycolumn) VALUES (@myvarbinaryvalue)", conn))
{
cmd.Parameters.Add("@myvarbinaryvalue", SqlDbType.VarBinary, myarray.length).Value = myarray;
cmd.ExecuteNonQuery();
}
ofc sqlcommand应打开且有效。
myarray的类型为byte[]您有什么问题?你在db交互方面有问题吗?嘿Georgy Smirnov..图像是以.jpg格式保存的…我需要将其转换为字节格式看看那里你可以看到,如何将其转换为字节数组