如何在 C# 中扫描图像并以正常大小保存它
How to scan an image and save it with normal size in c#
我想扫描页面并自动保存。这段代码运行良好,但问题是创建然后保存的图像太大了!它创建了一个大小为 30Mb 的图像!
如何更改此代码以保存正常大小的图像?
这是我的代码:
谢谢
private void button7_Click(object sender, EventArgs e)
{
try
{
var deviceManager = new DeviceManager();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
{
continue;
}
lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
try
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect(); //Connect to the available scanner.
var ScanerItem = device.Items[1]; // select the scanner.
var imgFile = (ImageFile)ScanerItem.Transfer(FormatID.wiaFormatJPEG); //Retrive an image in Jpg format and store it into a variable.
var Path = @"C:\....\ScanImg.jpg"; // save the image in some path with filename.
if (File.Exists(Path))
{
File.Delete(Path);
}
imgFile.SaveFile(Path);
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
/////////////////////////////////////
}
试试这个方法转换原始扫描数据:
public Bitmap GetBitmapFromRawData(int w, int h, byte[] data)
{
Bitmap bmp = new Bitmap(w, h);
int i = 0;
for ( int y = 0; y < h; y++ )
{
for ( int x = 0; x < w; x++ )
{
int a = 255;
// We have inverted red and blue to get the correct scanned image
// else it is flipped up/down and right/left with bad colors
int b = data[i];
int g = data[i + 1];
int r = data[i + 2];
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
i += 3;
}
}
return bmp;
}
所以你的代码现在是:
private void button1_Click(object sender, EventArgs e)
{
try
{
var deviceManager = new DeviceManager();
for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ ) // Loop Through the get List Of Devices.
{
if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
{
continue;
}
lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
}
catch ( COMException ex )
{
MessageBox.Show(ex.Message);
}
try
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ ) // Loop Through the get List Of Devices.
{
if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect(); //Connect to the available scanner.
var ScanerItem = device.Items[1]; // select the scanner.
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\....\ScanImg.jpg"; // save the image in some path with filename.
if ( File.Exists(Path) )
{
File.Delete(Path);
}
bitmap.Save(Path, ImageFormat.Jpeg);
}
catch ( COMException ex )
{
MessageBox.Show(ex.Message);
}
我想扫描页面并自动保存。这段代码运行良好,但问题是创建然后保存的图像太大了!它创建了一个大小为 30Mb 的图像!
如何更改此代码以保存正常大小的图像?
这是我的代码:
谢谢
private void button7_Click(object sender, EventArgs e)
{
try
{
var deviceManager = new DeviceManager();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
{
continue;
}
lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
try
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect(); //Connect to the available scanner.
var ScanerItem = device.Items[1]; // select the scanner.
var imgFile = (ImageFile)ScanerItem.Transfer(FormatID.wiaFormatJPEG); //Retrive an image in Jpg format and store it into a variable.
var Path = @"C:\....\ScanImg.jpg"; // save the image in some path with filename.
if (File.Exists(Path))
{
File.Delete(Path);
}
imgFile.SaveFile(Path);
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
/////////////////////////////////////
}
试试这个方法转换原始扫描数据:
public Bitmap GetBitmapFromRawData(int w, int h, byte[] data)
{
Bitmap bmp = new Bitmap(w, h);
int i = 0;
for ( int y = 0; y < h; y++ )
{
for ( int x = 0; x < w; x++ )
{
int a = 255;
// We have inverted red and blue to get the correct scanned image
// else it is flipped up/down and right/left with bad colors
int b = data[i];
int g = data[i + 1];
int r = data[i + 2];
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
i += 3;
}
}
return bmp;
}
所以你的代码现在是:
private void button1_Click(object sender, EventArgs e)
{
try
{
var deviceManager = new DeviceManager();
for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ ) // Loop Through the get List Of Devices.
{
if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
{
continue;
}
lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
}
catch ( COMException ex )
{
MessageBox.Show(ex.Message);
}
try
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ ) // Loop Through the get List Of Devices.
{
if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect(); //Connect to the available scanner.
var ScanerItem = device.Items[1]; // select the scanner.
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\....\ScanImg.jpg"; // save the image in some path with filename.
if ( File.Exists(Path) )
{
File.Delete(Path);
}
bitmap.Save(Path, ImageFormat.Jpeg);
}
catch ( COMException ex )
{
MessageBox.Show(ex.Message);
}