I'm using selenium to go to x website and take screenshot
public static void TakeScreenshot(IWebDriver driver, int x, int y, int width, int height)
{
var name =
$#"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\{"screenshots"}\{Guid.NewGuid()}.{
ScreenshotImageFormat.Png
}";
Rectangle rect = new Rectangle(x, y, width, height);
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
var bitmapScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
var croppedArea = new Rectangle(rect.Location, rect.Size);
bitmapScreen.Clone(croppedArea, bitmapScreen.PixelFormat).Save(name);
}
and after saving that I'm trying to read text from that image using tesseract
var testImagePath = #".\Content\300.png";
var dataPath = #".\tessdata";
try
{
using (var tEngine = new TesseractEngine(dataPath, "eng", EngineMode.Default)) //creating the tesseract OCR engine with English as the language
{
using (var img = Pix.LoadFromFile(testImagePath)) // Load of the image file from the Pix object which is a wrapper for Leptonica PIX structure
{
using (var page = tEngine.Process(img)) //process the specified image
{
var text = page.GetText(); //Gets the image's content as plain text.
Console.WriteLine(text); //display the text
Console.WriteLine(page.GetMeanConfidence()); //Get's the mean confidence that as a percentage of the recognized text.
Console.ReadLine();
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Unexpected Error: " + e.Message);
}
but I'm getting that dummy text:
so I tried to rescale that image
bitmapScreen.SetResolution(300, 300);
as I found here
but result is the same
Related
Hello there please I am facing a problem with searching and replacing a whole word with iTextSharp in a PDF.In fact the algorithme compare chunk with my WholeWorld and the chunk contain only three or two words. So the algorithme didn't find my exact word.
Here is my code:
Program.cs
using testProject;
string sourceFile = #"C:\oldFile.pdf";
string descFile = #"C:\test.pdf";
PDFEdit pdfObj = new PDFEdit();
pdfObj.ReplaceTextInPDF(sourceFile, descFile, "client", "hello");
PDFEdit.cs:
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.Drawing;
namespace testProject
{
public class PDFEdit
{
/// <summary>
/// Find the text and replace in PDF
/// </summary>
/// <param name="sourceFile">The source PDF file where text to be searched</param>
/// <param name="descFile">The new destination PDF file which will be saved with replaced text</param>
/// <param name="textToBeSearched">The text to be searched in the PDF</param>
/// <param name="textToBeReplaced">The text to be replaced with</param>
public void ReplaceTextInPDF(string sourceFile, string descFile, string textToBeSearched, string textToBeReplaced)
{
ReplaceText(textToBeSearched, textToBeReplaced, descFile, sourceFile);
}
private void ReplaceText(string textToBeSearched, string textToAdd, string outputFilePath, string inputFilePath)
{
try
{
using (Stream inputPdfStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
using (Stream outputPdfStream2 = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//Opens the unmodified PDF for reading
PdfReader reader = new PdfReader(inputPdfStream);
//Creates a stamper to put an image on the original pdf
PdfStamper stamper = new PdfStamper(reader, outputPdfStream); //{ FormFlattening = true, FreeTextFlattening = true };
for (var i = 1; i <= reader.NumberOfPages; i++)
{
var tt = new MyLocationTextExtractionStrategy(textToBeSearched);
var ex = new PdfReaderContentParser(reader).ProcessContent(i, tt, new Dictionary<string, IContentOperator>()).GetResultantText();
// var ex = PdfTextExtractor.GetTextFromPage(reader, i, tt); // ex will be holding the text, although we just need the rectangles [RectAndText class objects]
foreach (var p in tt.myPoints)
{
//Creates an image that is the size i need to hide the text i'm interested in removing
Bitmap transparentBitmap = new Bitmap((int)p.Rect.Width, (int)p.Rect.Height);
transparentBitmap.MakeTransparent();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Bitmap(130, 20), BaseColor.BLACK);
//Sets the position that the image needs to be placed (ie the location of the text to be removed)
image.SetAbsolutePosition(p.Rect.Left, (p.Rect.Top - 8));
//Adds the image to the output pdf
stamper.GetOverContent(i).AddImage(image, true); // i stands for the page no.
PdfContentByte cb = stamper.GetOverContent(i);
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(bf, 7);
// write the text in the pdf content
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(1, textToAdd, p.Rect.Left + 10, p.Rect.Top - 6, 0);
cb.EndText();
}
}
//Creates the first copy of the outputted pdf
stamper.Close();
}
}
catch (Exception ex)
{
}
}
}
}
MyLocationTextExtractionStrategy.cs :
using iTextSharp.text.pdf.parser;
namespace testProject
{
internal class MyLocationTextExtractionStrategy : LocationTextExtractionStrategy
{
//Hold each coordinate
public List<RectAndText> myPoints = new List<RectAndText>();
//The string that we're searching for
public String TextToSearchFor { get; set; }
//How to compare strings
public System.Globalization.CompareOptions CompareOptions { get; set; }
public MyLocationTextExtractionStrategy(String textToSearchFor, System.Globalization.CompareOptions compareOptions = System.Globalization.CompareOptions.None)
{
this.TextToSearchFor = textToSearchFor;
this.CompareOptions = compareOptions;
}
//Automatically called for each chunk of text in the PDF
public override void RenderText(TextRenderInfo renderInfo)
{
base.RenderText(renderInfo);
//See if the current chunk contains the text
//var startPosition = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(renderInfo.GetText(), this.TextToSearchFor, this.CompareOptions);
Console.WriteLine(renderInfo.GetText());
var startPosition = -1;
if (this.TextToSearchFor.Contains(renderInfo.GetText()))
{
startPosition = 0;
}
Console.WriteLine(startPosition);
//If not found bail
if (startPosition < 0)
{
return;
}
//Grab the individual characters
var chars = renderInfo.GetCharacterRenderInfos().Skip(startPosition).Take(this.TextToSearchFor.Length).ToList();
//Grab the first and last character
var firstChar = chars.First();
var lastChar = chars.Last();
//Get the bounding box for the chunk of text
var bottomLeft = firstChar.GetDescentLine().GetStartPoint();
var topRight = lastChar.GetAscentLine().GetEndPoint();
//Create a rectangle from it
var rect = new iTextSharp.text.Rectangle(bottomLeft[Vector.I1], bottomLeft[Vector.I2], topRight[Vector.I1], topRight[Vector.I2]);
//Add this to our main collection
this.myPoints.Add(new RectAndText(rect, this.TextToSearchFor));
}
}
}
RectAndText.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testProject
{
internal class RectAndText
{
public iTextSharp.text.Rectangle Rect;
public String Text;
public RectAndText(iTextSharp.text.Rectangle rect, String text)
{
this.Rect = rect;
this.Text = text;
}
}
}
I want to load in a JPEG file and print it with Aspose.Pdf in C# (.net Framework 4.8). The code I currently have is:
public void PrintImage(string fileToPrint, string printerName, string jobName)
{
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileToPrint);
int h = srcImage.Height;
int w = srcImage.Width;
var doc = new Document();
var page = doc.Pages.Add();
var image = new Image();
image.File = (fileToPrint);
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
var viewer = new PdfViewer(doc);
PrintUsingViewer(viewer, printerName, jobName);
}
private static void PrintUsingViewer(PdfViewer viewer, string printerName, string jobName)
{
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
var ps = new System.Drawing.Printing.PrinterSettings();
var pgs = new System.Drawing.Printing.PageSettings();
ps.PrinterName = printerName;
viewer.PrinterJobName = jobName;
viewer.PrintDocumentWithSettings(pgs, ps);
viewer.Close();
}
When I save the document instead of printing and look at it, it seems fine (the image is added). However, when trying to print the image it is not printed and the page is just blank..
I would like to print without first saving the document as a PDF and then trying to print that saved PDF. Does anyone see what I am doing wrong?
The solution was adding this line of code
doc.ProcessParagraphs();
right after this line:
page.Paragraphs.Add(image);
So the code now becomes
public void PrintImage(string fileToPrint, string printerName, string jobName)
{
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(fileToPrint);
int h = srcImage.Height;
int w = srcImage.Width;
var doc = new Document();
var page = doc.Pages.Add();
var image = new Image();
image.File = (fileToPrint);
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
doc.ProcessParagraphs();
var viewer = new PdfViewer(doc);
PrintUsingViewer(viewer, printerName, jobName);
}
private static void PrintUsingViewer(PdfViewer viewer, string printerName, string jobName)
{
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
var ps = new System.Drawing.Printing.PrinterSettings();
var pgs = new System.Drawing.Printing.PageSettings();
ps.PrinterName = printerName;
viewer.PrinterJobName = jobName;
viewer.PrintDocumentWithSettings(pgs, ps);
viewer.Close();
}
Now the image is printed correctly!
I am trying to create a Windows app which uploads files to FTP. Essentially, it looks for .jpeg files in a given folder, it reads through the barcodes found in the .jpg files before uploading it into the FTP server, and entering the URL into the database for our records.
As there will be multiple files at any given time in the folder, I am essentially trying to read them in a loop, and process them accordingly. However, I get an OutOfMemoryException whenever the loop starts again. I am trying to figure out what I'm doing wrong here. I have appended my code below:
private void btnProcess_Click(object sender, RoutedEventArgs e)
{
podPath = Directory.GetFiles(DestPath, "*.jpg");
List<string> scans = new List<string>(podPath.Length);
List<string> badscans = new List<string>();
byte[] imageBytes;
string filename, result;
POD conpod = new POD();
OTPOD otpod = new OTPOD();
ConsignmentObj scanJob;
//Pickup OTScan;
//Consolidate ccv;
for (int i = 0; i < podPath.Count(); i++ )
{
filename = podPath[i].ToString();
using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename))
{
var results = barcodeReader.Decode(bm);
result = results.ToString();
bm.Dispose();
}
if (result != null)
{
//if barcode can be read, we throw the value into the database to pull out relevant information
if (result.Contains(ConNotePrefix))
{
#region Consignments
scanJob = getCon(result.ToString());
final = ImageFolder + "\\" + result.ToString() + ".jpg";
using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
{
MemoryStream ms = new MemoryStream();
try
{
img.Save(ms, ImageFormat.Jpeg);
imageBytes = ms.ToArray();
img.Dispose();
}
finally
{
ms.Flush();
ms.Close();
ms.Dispose();
}
}
lock (filename)
{
if (System.IO.File.Exists(filename))
{
File.Delete(filename);
}
}
using (var stream = File.Create(final)) { }
File.WriteAllBytes(final, imageBytes);
File.Delete(filename);
conpod.ConsignmentID = scanJob.ConsignmentID;
conpod.UserID = 1;
conpod.Location = ftpUrl + "//" + result.ToString() + ".jpg";
conpod.rowguid = Guid.NewGuid();
UploadFilesToFtp(ftpUrl, ftpUser, ftpPass, final, result.ToString() + ".jpg");
insertPOD(conpod);
scans.Add(result.ToString());
#endregion
}
}
else
{
badscans.Add(filename);
}
}
this.lbScans.ItemsSource = scans;
this.lbBadScans.ItemsSource = badscans;
}
The FTP method, UploadFilesToFtp(x, x, x, x, x, x) is not a problem here. All feedback will be much appreciated.
An OutOfMemoryException can also be thrown by the method FromFile of the Image class when
The file does not have a valid image format.
or
GDI+ does not support the pixel format of the file.
So i think there is a problem with one of your image files you are reading. One solution is to catch the OutOfMemoryException and adding the file to the badscans.
try{
using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename)) {
var results = barcodeReader.Decode(bm);
result = results.ToString();
bm.Dispose();
}
}
catch(OutOfMemoryException) {
badscans.add(filename);
}
This is either very complicated or I am really bad!!
I am using AudioVideoCaptureDevice and I would like to create an image thumbnail of the video and save it into isolated storage.
So far what I have is the following:
private void SaveThumbnail()
{
var w = (int)_videoCaptureDevice.PreviewResolution.Width;
var h = (int)_videoCaptureDevice.PreviewResolution.Height;
var argbPx = new int[w * h];
Deployment.Current.Dispatcher.BeginInvoke(() => _videoCaptureDevice.GetPreviewBufferArgb(argbPx));
var wb = new WriteableBitmap(w, h);
argbPx.CopyTo(wb.Pixels, 0);
wb.Invalidate();
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var fileName = _isoVideoFileName + ".jpg";
if (isoStore.FileExists(fileName))
isoStore.DeleteFile(fileName);
var file = isoStore.CreateFile(fileName);
wb.SaveJpeg(file, w, h, 0, 20);
file.Close();
}
}
So the GetPreviewBufferArgb() method fills my int array with the image data (as the documentation states at least). How should I proceed to save those pixels into isolatedstorage so I could load them later on?
The above code doesn't seem to work since when I open the image from the IsolatedStorage it's not opening.
UPDATE: Now I am able to save something - unfortunately the image is always black (NO - I am not testing the application on the emulator)!!
Got it to work - a bit wired though.. it seemed like the whole processes should be done in a UI thread.
private void SaveThumbnail()
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var w = (int)_videoCaptureDevice.PreviewResolution.Width;
var h = (int)_videoCaptureDevice.PreviewResolution.Height;
var argbPx = new Int32[w * h];
_videoCaptureDevice.GetPreviewBufferArgb(argbPx);
var wb = new WriteableBitmap(w, h);
argbPx.CopyTo(wb.Pixels, 0);
wb.Invalidate();
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var fileName = _isoVideoFileName + ".jpg";
if (isoStore.FileExists(fileName))
isoStore.DeleteFile(fileName);
var file = isoStore.CreateFile(fileName);
wb.SaveJpeg(file, w, h, 0, 20);
file.Close();
}
});
}
I'm trying to insert images in my rich text box in C#, but so far I'm only failing. Miserably.
This is the code that I am using:
Clipboard.SetImage(Image.FromFile(Application.StartupPath + #"\PIC\" + i + ".bmp"));
chat.Paste();
The real problem is I am not able to put both text and image in the textbox. The moment I insert text after copying the image the image disappears. I am unable to find a solution for this
Can anybody help me with this? Please???
Thanks
RichTextBox rtb = new RichTextBox();
byte[] headerImage = (byte[])(dr["ImageData"]);
string imageData = string.Empty;
if (headerImage != null && headerImage.Length > 0)
{
Bitmap bmp = new Bitmap(new MemoryStream(headerImage));
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
imageData = #"{\pict\jpegblip\picw10449\pich3280\picwgoal9924\pichgoal1860\ " + BitConverter.ToString(ms.ToArray()).Replace("-", string.Empty).ToLower() + "}";
ms.Dispose();
}
string finalrtfdata = rtb.Rtf;
finalrtfdata = finalrtfdata.Replace("&ImageHeader&", imageData);
// finalrtfdata contain your image data along with rtf tags.
Try this out,you can paste this into your code and call it:
place a picture into your project to embeded resource,and call this method
passing the richtextbox.
private void createImage(Control item)
{
Hashtable image = new Hashtable(1);
image.Add(item,yourproject.Properties.Resources.yourpicturename);
Clipboard.SetImage((Image)image[item]);
((RichTextBox)item).Paste();
}
private static void createImage(RichTextBox item)
{
var image = new Hashtable(1) { { item, Properties.Resources.yourimage } };
Clipboard.SetImage((Image)image[item]);
item.Paste();
}