[英]Scan an image and save it like pdf into database c#
我正在創建一種掃描文檔並將其保存為圖像的方法。 然后它會創建一個 pdf 文檔,我在其中放置圖像並將其正確保存到數據庫中。 問題是我只想在我的文件瀏覽器中擁有 pdf,但是掃描的圖像也被保存了,我無法刪除它。 它給了我錯誤:“該操作無法完成,因為該文件已在另一個程序中打開”。
我使用 Visual Studio 2019 和 SQL 服務器。
有沒有另一種方法可以將 pdf 保存到數據庫中而無需保存圖像?
這是代碼:
internal static string EscanearDocumento(string pathImage, string path, PdfDocument doc)
{
int count = 0;
WIA.CommonDialog wiaDiag = new WIA.CommonDialog();
var imageFile = wiaDiag.ShowAcquireImage(WiaDeviceType.ScannerDeviceType, WiaImageIntent.TextIntent, WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", true, true, false);
while (File.Exists(pathImage + cont + ".png") || File.Exists(path + cont + ".pdf"))
{
cont++;
}
pathImage = pathImage + cont + ".png";
path = path + cont + ".pdf";
try
{
imageFile.SaveFile(pathImage);
Image img = Image.FromFile(pathImage);
PdfPage page = doc.AddPage();
page.Orientation = PageOrientation.Portrait;
XGraphics graphics = XGraphics.FromPdfPage(page);
graphics.DrawImage(XImage.FromFile(pathImage), 0, 0);
img.Dispose();
} catch(NullReferenceException)
{
MessageBox.Show("Error");
}
這是我用來掃描文檔的方法:
internal static string EscanearDocumento(string path, PdfDocument doc)
{
int cont = 0;
WIA.CommonDialog wiaDiag = new WIA.CommonDialog();
try
{
ImageFile imageFile = wiaDiag.ShowAcquireImage(WiaDeviceType.ScannerDeviceType, WiaImageIntent.TextIntent, WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", true, true, false);
while (File.Exists(path + cont + ".pdf"))
{
cont++;
}
path = path + cont + ".pdf";
try
{
byte[] imagenEnBytes = (byte[])imageFile.FileData.get_BinaryData();
using (MemoryStream ms = new MemoryStream(imagenEnBytes))
{
XImage imagen = XImage.FromStream(ms);
PdfPage page = doc.AddPage();
page.Orientation = PageOrientation.Portrait;
XGraphics graficos = XGraphics.FromPdfPage(page);
graficos.DrawImage(imagen, 0, 0);
}
} catch(NullReferenceException)
{
MessageBox.Show("Error");
}
} catch (COMException er) {
excepciones(er);
}
return path;
}
我在我的主要 class 中使用它,使用這種方法將文檔插入數據庫:
private void AgregarDocumentos_Click(object sender, EventArgs e)
{
string text1= TextBox1.Text;
string text2= TextBox2.Text;
string query = "SELECT * FROM tableName WHERE p ='" + text1 + "'AND n ='" + text2+ "'AND documento is not null";
SqlDataAdapter adapter = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows.Count == 0)
{
Boolean found = false;
try
{
DeviceManager manejadorDisp = new DeviceManager();
DeviceInfo escanerDisponible = null;
for (int i = 1; i <= manejadorDisp.DeviceInfos.Count; i++)
{
if (manejadorDisp.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
else
{
found = true;
string path = @"C:";
escanerDisponible = manejadorDisp.DeviceInfos[i];
Device dispositivo = escanerDisponible.Connect();
Item objetoEscaneado = dispositivo.Items[1];
PdfDocument doc = new PdfDocument();
try
{
path = Escaner.EscanearDocumento(path, doc);
try
{
DialogResult dialogResult = DialogResult.Yes;
while (dialogResult == DialogResult.Yes)
{
dialogResult = MessageBox.Show("¿Quiere escanear un documento?", "", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Escaner.EscanearDocumento(path, doc);
doc.Close();
}
else if (dialogResult == DialogResult.No)
{
doc.Save(path);
continue;
}
}
var leer = File.ReadAllBytes(path);
SqlCommand inserta = new SqlCommand("UPDATE tableName SET n = n, p = p, ni = ni, documento = @documento WHERE p ='" + text1 + "'AND n ='" + text2 + "';", con);
inserta.Parameters.AddWithValue("@documento", read);
con.Open();
inserta.ExecuteNonQuery();
con.Close();
Process proceso = new Process();
proceso.StartInfo = new ProcessStartInfo(path)
{
UseShellExecute = true
};
proceso.Start();
MessageBox.Show("Inserted");
break;
}
catch (InvalidOperationException)
{
MessageBox.Show("Cannot save the file");
}
}
catch (FileNotFoundException)
{
MessageBox.Show("Not found");
}
}
}
}
catch (COMException er)
{
Escaner.excepciones(er);
}
if(!found)
{
MessageBox.Show("Not found");
}
}
else
{
MessageBox.Show("");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.