|
if i write the code below, it shows me the message box when ever the device is empty!
catch (COMException ex)
{
uint errorCode = (uint)ex.ErrorCode;
if (errorCode == 0x80210003)
{
MessageBox.Show("Load the scaner device!");
}
else
{
MessageBox.Show(ex.Message);
}
but i do NOT want to see this message when the scan proccess will be finished.
(i want to see nothing when all papers scanned. just see a message if at the beginning i click and the device was empty)
Could you pls correct it?
|
|
|
|
|
How does the application know that all the pages have been scanned?
|
|
|
|
|
According to the Windows Imaging Acquisition codes, that means the paper is empty. If you reach this point, it suggests that you have finished scanning. Something you need to consider; your while loop will never end until you hit an exception because you aren't setting it to false anywhere. Also, your logic looks very odd. You're evaluating the scanners each time through the while loop, and connecting again and again. You should move the for loop and the AvailableScanner.Connect outside the while loop; something like this perhaps:
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner == null)
{
return;
}
var device = AvailableScanner.Connect();
while (continueRunning2)
{
var ScanerItem = device.Items[1];
...
}
modified 19-Nov-19 8:08am.
|
|
|
|
|
Thanks but it didn't work. now the problem is with if statement which you added.
according to your suggetion, i changed it like below:
could you please say where is the problem?
int n = 1;
bool continueRunning2 = true;
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner != null)
{
return;
}
var device = AvailableScanner.Connect();
while (continueRunning2)
{
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Your version doesn't match mine. Check what's in my answer, I edited it as soon as I'd posted it because I noticed the problem with the if condition. The if statement says:
if (AvailableScanner == null)
|
|
|
|
|
A point to note. You still have the problem that you aren't actually setting your boolean condition to false anywhere so you're still going to throw the exception when the scanner finishes scanning documents.
|
|
|
|
|
i changed it but it still has the same problem as before (it shows the error of loading paper).
Below is my complete code, could you please check/correct it?
try
{
object[] items = await Task.Run<object[]>(() =>
{
var deviceManager = new DeviceManager();
List<object> result = new List<object>();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
result.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
return result.ToArray();
});
foreach (var item in items)
{
lstListOfScanner.Items.Add(item);
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
int n = 1;
bool continueRunning2 = true;
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner == null)
{
return;
}
var device = AvailableScanner.Connect();
while (continueRunning2)
{
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Your problem is that you are making the scope of your try/catch for the COMException too broad. As you only want to show the message if you haven't managed to successfully scan a single page, then all you really need to do is track whether or not you have scanned a page and show the message if you haven't. This could be done simply by using a boolean to track whether or not the page has loaded.
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner == null)
{
return;
}
var device = AvailableScanner.Connect();
bool hasScannedPage = false;
while (continueRunning2)
{
var ScanerItem = device.Items[1];
ImageFile imgFile = null;
try
{
imgFile = (ImageFile)ScanerItem.Transfer();
}
catch (COMException ex)
{
uint errorCode = (uint)ex.ErrorCode;
if (errorCode == 0x80210003 && !hasScannedPage)
{
MessageBox.Show("You cannot scan anything without loading the device");
}
break;
}
hasScannedPage = true;
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
|
|
|
|
|
Perfect! Thanks!
It was what i need!
|
|
|
|
|
You're welcome. I'm glad it's working now.
|
|
|
|
|
Dear Pete,
I have also another short question. i will appreciate it if you tell me how to fix this:
1)I have a button to load a photo into a picturebox and extract the code into a text field.(It works well)
2)There is also another button for deleting that image which i mentioned. But when i click on delete button it couldnt delete the image because it said: "Its being used by another process"
I dont know how to delete this image from directory after i saw the qrcode in textbox.
Here are my code:
Thanks
private void button2_Click(object sender, EventArgs e)
{
var Path = @"D:\1.jpg";
pictureBox1.Image = Image.FromFile(Path);
MessagingToolkit.QRCode.Codec.QRCodeDecoder decoder = new MessagingToolkit.QRCode.Codec.QRCodeDecoder();
textBox2.Text = decoder.Decode(new QRCodeBitmapImage(pictureBox1.Image as Bitmap));
}
private void button3_Click(object sender, EventArgs e)
{
if (File.Exists(@"D:\1.jpg"))
{
System.IO.File.Delete(@"D:\1.jpg");
}
}
|
|
|
|
|
The thing that is locking the image is the picturebox (specifically, the Image). You'll have to unload that first.
|
|
|
|
|
and where should i make the boolean to false?
Thanks
|
|
|
|
|
Quote: I want to insert data into sqlserver but when I click on each textbox such as: First Name, Last Name, Gender ... instead of typing, I finished reading and then press the insert button will save SQLServer, working on C # . thanks
|
|
|
|
|
|
I run the code below only the code "en-US, en-GB" I want to add the code "vi-VN" in win7, what should I do ?
foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers())
{
System.Diagnostics.Debug.WriteLine(ri.Culture.Name);
}
|
|
|
|
|
By default, Windows does not provide support for Vietnamese as a speech recognition language. If you want to use Vietnamese, you might have to consider using something like ESpeak[^] instead.
|
|
|
|
|
|
You do realise that the Language support you point to is for the Azure Cognitive Services version don't you? That's a paid-for cloud service, and not the one you have through the SpeechRecognitionEngine . You can use the one I linked to via P/Invoke.
|
|
|
|
|
Hi Pete O'Hanlon!
I don't think the 2 pages I resend charge for one microsoft page, the other one of Azure, thank you for answering my questions. In case other countries do not support voice pronunciation like Vietnam, those countries often use which programming language ? Is there any library for C# programming support ?
|
|
|
|
|
I have list of information with following fields:
Project State Title
ABC Resolved Title1
ABC Pending Title2
DEF Archived Title3
DEF Resolved Title4
DEF Committed Title5
DEF Active Title6
I want output in following format using c#
Project Pending Resolved Committed Active
ABC 2 1 0 3
DEF 1 3 1 15
private void button2_Click(object sender, EventArgs e)
{
List<Item> objItems = new List<Item>();
objItems.Add(new Item(1, "ABC", "Title1", "Resolved"));
objItems.Add(new Item(2, "ABC", "Title2", "Pending"));
objItems.Add(new Item(3, "DEF", "Title3", "Archived"));
objItems.Add(new Item(4, "DEF", "Title4", "Resolved"));
objItems.Add(new Item(5, "DEF", "Title5", "Committed"));
objItems.Add(new Item(6, "DEF", "Title6", "Active"));
objItems.Add(new Item(7, "ABC", "Title2", "Pending"));
var data = objItems.Pivot(c => c.Project, c => c.State, lst => lst.Sum(c=> c.State)).ToList();
dataGridView1.DataSource = data;
}
public class Item
{
public int Id { get; set; }
public string Project { get; set; }
public string Title { get; set; }
public string State { get; set; }
public Item(int id, string project, string title, string state)
{
this.Id = id;
this.Project = project;
this.Title = title;
this.State = state;
}
}
public static class Extension
{
public static DataTable ToPivotTable<T, TColumn, TRow, TData>(
this IEnumerable<T> source,
Func<T, TColumn> columnSelector,
Expression<Func<T, TRow>> rowSelector,
Func<IEnumerable<T>, TData> dataSelector)
{
DataTable table = new DataTable();
var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
table.Columns.Add(new DataColumn(rowName));
var columns = source.Select(columnSelector).Distinct();
foreach (var column in columns)
table.Columns.Add(new DataColumn(column.ToString()));
var rows = source.GroupBy(rowSelector.Compile())
.Select(rowGroup => new
{
Key = rowGroup.Key,
Values = columns.GroupJoin(
rowGroup,
c => c,
r => columnSelector(r),
(c, columnGroup) => dataSelector(columnGroup))
});
foreach (var row in rows)
{
var dataRow = table.NewRow();
var items = row.Values.Cast<object>().ToList();
items.Insert(0, row.Key);
dataRow.ItemArray = items.ToArray();
table.Rows.Add(dataRow);
}
return table;
}
public static Dictionary<TKey1, Dictionary<TKey2, TValue>> Pivot<TSource, TKey1, TKey2, TValue>(
this IEnumerable<TSource> source
, Func<TSource, TKey1> key1Selector
, Func<TSource, TKey2> key2Selector
, Func<IEnumerable<TSource>, TValue> aggregate)
{
return source.GroupBy(key1Selector).Select(
x => new
{
X = x.Key,
Y = source.GroupBy(key2Selector).Select(
z => new
{
Z = z.Key,
V = aggregate(from item in source
where key1Selector(item).Equals(x.Key)
&& key2Selector(item).Equals(z.Key)
select item
)
}
).ToDictionary(e => e.Z, o => o.V)
}
).ToDictionary(e => e.X, o => o.Y);
}
}
This Pivot code i got from here https://stackoverflow.com/a/6282079
Tell me how to arrange my data using above Pivot function. please tell me what to rectify in code to get desire output. thanks
|
|
|
|
|
I just pasted this in Google, and lo' and behold, it is in QA, marked as "solved", and twice on SO.
The post in QA does a pivot, so you could expand on that; in the example you give I do not see where the numbers are to be coming from.
Perhaps you would like to rentacoder?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
As Eddy said, it's not clear where those numbers are coming from. But the obvious error is that you can't Sum a string column.
Try replacing lst => lst.Sum(c=> c.State) with lst => lst.Count() .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
public void Test()
{
/* Flow board variable declaration */
UInt64 frpHandle = 0;
ushort Serial = 0;
ushort Version = 0;
/* Flow-rate acquisition variables */
byte sensor_index = 0; // sensor index coresponds to flow-unit port on the flowboard from 0 to 7
byte TimeCheck = 0;
float flow_rate = 0;
uint loop = 0;
frpHandle = frp_initialization(0);
RTBox.Text="\n FRP session initialized";
RTBox.Refresh();
frp_get_serial(frpHandle, ref Serial, ref Version);
RTBox.Text = ("\n FLOWBOARD SN:" + Convert.ToInt32(Serial));
RTBox.Refresh();
if (Serial != 0)
{
for (loop = 0; loop < 20; loop++)
{
frp_read_flow(frpHandle, sensor_index, ref TimeCheck, ref flow_rate);
RTBox.Text = ("\n Flow-rate:" + (flow_rate) + "\t ul/min");
RTBox.Refresh();
}
Thread.Sleep(100);
};
frp_close(frpHandle);
RTBox.Text = ("\n FRP session closed");
RTBox.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
Test();
}
|
|
|
|
|
No, they won't. And there are loads of reasons why not.
Firstly, you set the RTB Text each time - which discards any text in it already, so only the last text will ever be displayed.
Secondly, this is code that runs on the UI thread - if it didn't, you would get a "Cross-threading exception" when you set the Text property - so it blocks the thread which is in charge of changing the display which means that the RTB content can't be drawn until your method exits.
Thirdly, you are waiting for serial input data, which blocks the method from doing anything else - and that further blocks the UI thread.
What you need to do is move that code into a separate thread and receive updates from the thread to display data in your UI thread.
I'd suggest a BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^] - it's easy to use and provides progress reporting back to the UI thread via an Event which you can use to update your RTB as it goes along.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
modified 16-Nov-19 2:13am.
|
|
|
|
|