I have code that scan file and save it on my path:
public ImageFile Scan()
{
var device = this._deviceInfo.Connect();
var item = device.Items[1];
var imageFile = (ImageFile)item.Transfer(FormatID.wiaFormatJPEG);
return imageFile;
}
protected void btnscan_Click(object sender, EventArgs e)
{
foreach (Object scanner in scanners)
{
if (ddlSelectDevice.SelectedItem.ToString().Equals(scanner.ToString()))
{
//var d = ddlSelectDevice.SelectedIndex as Scanner;
var device = scanner as Scanner;
if (device == null)
{
return;
}
var image = device.Scan();
var path = "~/Scanner/scan.jpeg";
if (File.Exists(Server.MapPath(path)))
{
File.Delete(Server.MapPath(path));
}
image.SaveFile(Server.MapPath(path));
break;
}
}
}
I save this image in database by convert to byte:
private byte[] image2Byte()
{
string filePath = null;
Byte[] bytes = null;
if (File.Exists(Server.MapPath("~/Scanner/scan.jpeg")))
{
filePath = Server.MapPath("~/Scanner/scan.jpeg");
return bytes = File.ReadAllBytes(filePath);
}
else
{
filePath = Server.MapPath("~/Scanner/None.jpg");
return bytes = File.ReadAllBytes(filePath);
}
}
But my images usually have large size (up 400kB) and makes problem on load. How I can reduce image size?
You can use ImageProcessor to reduce the quality of the JPEG file. A quality of 80% is accaptable for most use cases.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData:true))
{
imageFactory.Load(inStream)
.Quality(80)
.Save(outStream);
}