C# 如何扫描图像并将其以正常大小保存在c中#
我想扫描一个页面并自动保存它。这段代码运行得很好,但问题是创建的图像太大,然后保存!它创建了一个30Mb大小的图像! 如何更改此代码以保存正常大小的图像? 这是我的代码:C# 如何扫描图像并将其以正常大小保存在c中#,c#,image-resizing,scanning,image-size,save-image,C#,Image Resizing,Scanning,Image Size,Save Image,我想扫描一个页面并自动保存它。这段代码运行得很好,但问题是创建的图像太大,然后保存!它创建了一个30Mb大小的图像! 如何更改此代码以保存正常大小的图像? 这是我的代码: 谢谢 private void按钮7\u单击(对象发送者,事件参数e) { 尝试 { var deviceManager=new deviceManager(); 对于(int i=1;i尝试此方法转换原始扫描数据: public Bitmap GetBitmapFromRawData(int w, int h, byte[]
谢谢
private void按钮7\u单击(对象发送者,事件参数e)
{
尝试
{
var deviceManager=new deviceManager();
对于(int i=1;i尝试此方法转换原始扫描数据:
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;
}
公共位图GetBitmapFromRawData(int w,int h,byte[]数据)
{
位图bmp=新位图(w,h);
int i=0;
对于(int y=0;y
因此,您的代码现在是:
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);
}
private void按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
var deviceManager=new deviceManager();
对于(int i=1;我不知道如何将该代码添加到我的代码中。ypu是否可以添加该代码?好的,在使用之前需要从ImageFile对象中获取图像对象。ScanerItem程序集引用是什么?好的,谢谢。如果可以连接这两个代码,那将非常好:)我用过WIA;用过System.Runtime.InteropServices;用过System.Drawing;用过System.IO;用过System;看看答案。谢谢你的新代码,但它现在不起作用了。它只有一个问题:这一行上的“数据”:var bitmap=GetBitmapFromRawData(imgFile.Width,imgFile.Height,data);错误是:无法从“object”转换为“byte[]“我如何修复它?我已经重新粘贴了代码并添加了一个无用的cast in case,但如果您复制相同的方法来替换代码中的旧方法,它应该会起作用。感谢有用的代码。当我尝试使用新项目时,它起作用了,但当我尝试使用我的项目时,它显示了错误。我明天会继续处理它,并将反馈发送回我的com上。”使用var data=(byte[])imgFile.FileData.get_BinaryData();
并检查保存文件的路径是否存在或创建文件,否则会导致GDI+异常…在扫描黑白时一切正常。我只是尝试了一下颜色(上次使用黑白).事实上,现在图像像你说的那样上下左右翻转,颜色很可怕:绿色是绿色,但红色是蓝色,蓝色是黄色/橙色…这真的很奇怪。我不知道WIA,但GetBitmapFromRawData
有问题,所以我颠倒了红色和蓝色的值,现在图像很完美。黑白仍然是g好的。更新答案。谢谢。
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);
}