Aspose OCR - Not Recognising Image - c#

I'm playing around with Aspose OCR and it's reading images with white backgrounds and black text fine. However, anything with a black or different colour background it does not read.
Any idea how to get around this?
private void timer1_Tick(object sender, EventArgs e)
{
//SendKeys.Send("{PRTSC}");
Image img = Clipboard.GetImage();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = img;
if (img != null)
{
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg); // put here the image format
ms.Position = 0;
ocr.Image = ImageStream.FromStream(ms, ImageStreamFormat.Jpg);
if (ocr.Process()) // Start processing it
{
label1.Text = "Text: " + ocr.Text;
}
}
}

You may try the following line of code. Further to update you that development on processing of color back ground image feature is in-progress.
ocr.Config.ProcessColoredBackground = true;
I work with Aspose as Developer evangelist.

Related

exception 'System.ArgumentException'

I am loading the data from MySQL in DataGridView and trying to to show the data from grid view to the textboxes and picture box. When I am trying to load the image from datagridview to the picture box it throws an exception. I have wasted my 2 days to resolve it but I remain unsuccessful.
This is the exception which I get when I click on the cell:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
id.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
cost.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
price.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
status.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
description.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
//picture.Image = (Image)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
if (dataGridView1.Rows[e.RowIndex].Cells[5].Value != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])dataGridView1.Rows[e.RowIndex].Cells[5].Value);
picture.Image = Image.FromStream(ms); // here is exception
}
else
{
picture.Image = null;
}
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
If binary representation of the image is malformed, you'll get this error:
var imageBytes = Convert.FromBase64String(#"Qk16AAAAAAAAAHYAAAAoAAAAAQAAAAEAAAABAAQAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAgAAAAICAAIAAAACAAIAAgIAAAICAgADAwMAAAAD/AAD/AAAA//8A/wAAAP8A/wD//wAA////APAAAAA=");
var img = Image.FromStream(new MemoryStream(imageBytes)); // ok
var badImageBytes = Convert.FromBase64String(#"Qk16AAAAAAAAAHYAAAAoAAAAAQAAAAEAAAABAAQAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAgAAAAICAAIAAAACAAIAAgIAAAICAgADAwMAAAAD/AAD/AAAA//8A/wAAAP8A/wD//wAA////APAAAA==");
var badImg = Image.FromStream(new MemoryStream(badImageBytes )); // not ok
The simplest way to check consistency of the value will be to save it to the file and try open with any image viewer.
var bytes = (byte[])dataGridView1.Rows[e.RowIndex].Cells[5].Value;
File.WriteAllText(#"C:\Temp\image.base64.txt", Convert.ToBase64String(bytes)); // if image is not confidential you may share it with the community in the comment
File.WriteAllBytes(#"C:\Temp\image.bmp", bytes);
Please note that only following image formats are supported:
BMP
GIF
JPEG
PNG
TIFF
The reason why it is malformed could be different:
someone uploaded non-valid image
there are some data transformation (compression/encryption)
data was truncated in the storage (check DDL of the column if it is SQL)

Delete or have Control of an Image while your Application is Using it C#

I’m using WinForms. In my form I have a picturebox. On form load, my program opens an image document into my picturebox from my C:/image directory. The problem is when my program opens that image I cannot go into my C:/image directory and delete this picture because my application is using it. When I go to C:/image directory and try to delete the picture I get this error.
My goal is to have control over the image document that means i have the ability to delete the specific document even if its being used by my application.
Test: I tested if you can delete an image while viewing it at the same time with "Windows Photo Viewer" installed into in my computer, and that application lets you. Windows photo viewer doesn't lock the images. When you delete an image from the directory the image goes away in windows photo viewer as well. I want to accomplish something similar.
Suggested code: I tried implementing this but, i think i'm implementing it incorrectly.
Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
img = new Bitmap(bmpTemp);
}
Below i provided my code i wrote to load the picture into my picture box.
private void Form1_Load(object sender, EventArgs e) //When form load you do this:
{
try // Get the tif file from C:\image\ folder
{
string path = #"C:\image\";
string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.
pictureBox1.Load(filename[0]);
lblFile.Text = filename[0];
RefreshImage(); // refreshing and showing the new file
opened = true; // the files was opened.
Image img1 = Image.FromFile(lblFile.Text);
pictureBox1.Image = img1;
pictureBox1.Width = img1.Width;
pictureBox1.Height = img1.Height;
picWidth = pictureBox1.Width;
picHeight = pictureBox1.Height;
getRatio();
}
catch (Exception ex)
{
MessageBox.Show("No files or " + ex.Message);
}
}
Make a copy of the image file bits before creating the image:
private void Form1_Load(object sender, EventArgs e) //When form load you do this:
{
try // Get the tif file from C:\image\ folder
{
string path = #"C:\image\";
string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.
FileInfo fi = new FileInfo(filename[0]);
byte [] buff = new byte[fi.Length];
using ( FileStream fs = File.OpenRead(fileToDisplay) )
{
fs.Read(buff, 0, (int)fi.Length);
}
MemoryStream ms = new MemoryStream(buff);
Bitmap img1 = new Bitmap(ms);
opened = true; // the files was opened.
pictureBox1.Image = img1;
pictureBox1.Width = img1.Width;
pictureBox1.Height = img1.Height;
picWidth = pictureBox1.Width;
picHeight = pictureBox1.Height;
getRatio();
}
catch (Exception ex)
{
MessageBox.Show("No files or " + ex.Message);
}
}

Delete an image being used by another process

I am writing a winform application in C# to open an image and overlay another image on top of it.
The bottom image is a .jpg and the top one is a .bmp converted from .svg. The .jpg and the .svg are the only ones I want to keep in the folder. The .bmp works as a temp.
I was using the following code to overlay the images. But I am having trouble to delete the temp .bmp as it is used by another process. I think it is this combine code still have access to the last .bmp file.
Could anyone help me on this? Thanks.
private void buttonSearch_Click(object sender, EventArgs e)
{
FailInfo.Text = "";
deletebmp(strFolderPath);
...
// Check if the specified front image exists. Yes, show the file name and convert SVG to BMP. No, show the error msg.
if (File.Exists(strFilePathF))
{
labelFront.Text = strFileNameF;
var svgConvert = SvgDocument.Open(svgFilePathF);
svgConvert.Draw().Save(bmpFilePathF);
pictureBoxFront.Image = Image.FromFile(strFilePathF);
}
else
{
labelFront.Text = "Couldn't find the file!";
pictureBoxFront.Image = null;
}
// Check if the specified back image exists. Yes, show the file name and convert SVG to BMP. No, show the error msg.
if (File.Exists(strFilePathBF))
{
labelBack.Text = strFileNameBF;
strFilePathB = strFilePathBF;
pictureBoxBack.Image = Image.FromFile(strFilePathB);
labelResult.Text = "FAIL";
labelResult.BackColor = Color.FromArgb(255, 0, 0);
var svgConvert = SvgDocument.Open(svgFilePathBF);
bmpFilePathB = strFolderPath + strFileNameBF + ".bmp";
svgConvert.Draw().Save(bmpFilePathB);
svgFilePathB = svgFilePathBF;
inspectionres(svgFilePathB);
labelreason.Visible = true;
}
else if (File.Exists(strFilePathBP))
{
labelBack.Text = strFileNameBP;
strFilePathB = strFilePathBP;
pictureBoxBack.Image = Image.FromFile(strFilePathB);
labelResult.Text = "PASS";
labelResult.BackColor = Color.FromArgb(0, 255, 0);
var svgConvert = SvgDocument.Open(svgFilePathBP);
bmpFilePathB = strFolderPath + strFileNameBP + ".bmp";
svgConvert.Draw().Save(bmpFilePathB);
svgFilePathB = svgFilePathBP;
inspectionres(svgFilePathB);
labelreason.Visible = false;
}
else
{
labelBack.Text = "Couldn't find the file!";
pictureBoxBack.Image = null;
labelResult.Text = "ERROR";
labelResult.BackColor = Color.FromArgb(0, 255, 255);
labelreason.Visible = false;
}
}
//
// Overlay the SVG file on top of the JPEG file
//
private Bitmap Combine(string jpegFile, string bmpFile)
{
Image image1 = Image.FromFile(jpegFile);
Image image2 = Image.FromFile(bmpFile);
Bitmap temp = new Bitmap(image1.Width, image1.Height);
using (Graphics g = Graphics.FromImage(temp))
{
g.DrawImageUnscaled(image1, 0, 0);
g.DrawImageUnscaled(image2, 0, 0);
}
return temp;
}
//
// Show the overlaid graphic in the picturebox
//
private void checkBoxOverlay_CheckedChanged(object sender, EventArgs e)
{
try
{
if (FindFront)
if (checkBoxOverlay.Checked)
pictureBoxFront.Image = Combine(strFilePathF, bmpFilePathF);
else
pictureBoxFront.Image = Image.FromFile(strFilePathF);
else
pictureBoxFront.Image = null;
if (FindBack)
if (checkBoxOverlay.Checked)
pictureBoxBack.Image = Combine(strFilePathB, bmpFilePathB);
else
pictureBoxBack.Image = Image.FromFile(strFilePathB);
else
pictureBoxBack.Image = null;
}
catch (Exception ex)
{
MessageBox.Show("Error loading image" + ex.Message);
}
}
//
// Option of changing the image folder
//
private void buttonPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
strFolderPath = FolderBrowserDialog1.SelectedPath + "\\";
}
}
//
// Pull the inspection result info from the SVG file
//
private void inspectionres(string filename)
{
XDocument document = XDocument.Load(filename);
XElement svg_Element = document.Root;
string sb = null;
var faillist = (from svg_path in svg_Element.Descendants("{http://www.w3.org/2000/svg}text") select svg_path).ToList();
foreach (var item in faillist)
{
sb += item.ToString();
}
}
//
// Delete all the .bmp files generated from .svg files
//
private void deletebmp(string path)
{
// Unload the images from the picturebox if applicable
pictureBoxFront.Image = null;
pictureBoxBack.Image = null;
string[] files = Directory.GetFiles(path, "*.bmp");
for (int i = 0; i < files.Length; i ++ )
File.Delete(files[i]);
}
}
Image implements IDisposable, so simply setting the pictureBox.Image property to null will not release resources (in your case, the file). Your Combine method also leaves the images open. You have to call Dispose before attempting to delete the file:
Image image1 = Image.FromFile(path1);
File.Delete(path1); // error - file is locked
Image image2 = Image.FromFile(path2);
image2.Dispose();
File.Delete(path2); // works
An alternative approach (and I assume you're using WinForms here, in WPF it's a little different) would be to load the bitmap from the file manually (using FromStream). Then, you can close the stream immediately and delete the file:
Image image;
using (Stream stream = File.OpenRead(path))
{
image = System.Drawing.Image.FromStream(stream);
}
pictureBox.Image = image;
File.Delete("e:\\temp\\copy1.png"); //works
Vesan's answer didn't helped me so I found an different solution.
So I can safe/open an image and if I want instantly delete the image.
i used it for my dataGridView_SelectionChanged:
private void dataGridViewAnzeige_SelectionChanged(object sender, EventArgs e)
{
var imageAsByteArray = File.ReadAllBytes(path);
pictureBox1.Image = byteArrayToImage(imageAsByteArray);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
All above answers are perfectly fine, but I've got a different approach.
Using Image abstract class, you will not get quite a lot of options for manipulating and resizing image.
Rather you do as follows:-
Bitmap img = new Bitmap(item);
img.SetResolution(100, 100);
Image imgNew = Image.FromHbitmap(img.GetHbitmap());
pictureBox1.Image = imgNew;
img.Dispose();

Save App.RootFrame Background in wp8

I am using PhotoChooserTaskto pick a picture from photo gallery and then set this picture as my app background. Everything works fine, but when I exit my application it's not saved. What I am looking is to save this background for my application.
I am using below code:
photoChooserTask = new PhotoChooserTask()
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
imgCustom.Source = bmp;
System.Windows.Media.ImageBrush backgroundBrush = new System.Windows.Media.ImageBrush() { ImageSource = bmp };
App.RootFrame.Background = backgroundBrush;
}
}
What I am looking is, If I close and run my application, This background should not change unless the user changes it.
Thanks!
How about trying out this piece of code at the end:
var frame = App.Current.RootVisual as PhoneApplicationFrame;
frame.Background = backgroundBrush;
Reference: Windows Phone app background image

Deleting Image that's in PictureBox

I've searched and read a lot of the different ways to do this... and tried many of them.
When the program loads it loads printmark.png into the picture box.
Of course every time I try to delete the PNG file it says it's in use. As you can see I've tried both the Image.FileFrom and the picturebox.Load method.
This is the code I have.
private void GetCurrentLogos()
{
Image CurrentWM = Image.FromFile(#"C:\pics\logo.png");
Image CurrentPM = Image.FromFile(#"C:\pics\printmark.png");
pbWatermark.Image = CurrentWM;
pbPrintmark.Image = CurrentPM;
//pbWatermark.Load(#"C:\pics\logo.png");
//pbPrintmark.Load(#"C:\pics\printmark.png");
}
private void btnSetPM_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
txtNewPM.Text = item.Tag.ToString();
pbPrintmark.Image.Dispose();
pbPrintmark.Image = null;
pbPrintmark.Refresh();
Application.DoEvents();
renameMark("printmark.png", txtNewPM.Text);
}
}
private void renameMark(string MarkType, string FileName)
{
string path = txtPath.Text;
string FullSource = path + FileName;
string FullDest = path + MarkType;
if(File.Exists(FullDest))
{
File.Delete(FullDest);
}
System.IO.File.Copy(FullSource, FullDest);
}
See Image.FromFile():
The file remains locked until the Image is disposed.
To get around this, pass the returned image to a new Bitmap so that the original lock gets released:
Image tmp = Image.FromFile(#"C:\pics\logo.png");
Image CurrentWM = new Bitmap(tmp);
tmp.Dispose();
As noted in some of the answers to this question, an image created by Image.FromFile keeps the underlying file open. This is by design, as the MSDN documentation says ("The file remains locked until the Image is disposed."). You can get around this by loading the file into a MemoryStream, then creating the image from that stream.
tmp.Dispose(); didnt worked for me so maybe u need also a different solution.
I used it for my dataGridView_SelectionChanged:
private void dataGridViewAnzeige_SelectionChanged(object sender, EventArgs e)
{
var imageAsByteArray = File.ReadAllBytes(path);
pictureBox1.Image = byteArrayToImage(imageAsByteArray);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

Categories

Resources