I'm trying to convert a pdf into a PNG, using the code given in the following example (the first function) : https://ghostscriptnet.codeplex.com/SourceControl/latest#Ghostscript.NET/Ghostscript.NET.Samples/Samples/DeviceUsageSample.cs
However, I get this error on launch : " An error occured when call to 'gsapi_init_with_args' is made: -100"... which doesn't mean a lot.
How comes this basic example doesn't work ? I downloaded the latest Ghostscript.NET.dll here : https://ghostscriptnet.codeplex.com/ and added it to the references of the project. My OS is Windows 7 x32 bits and I run VisualStudio as an administrator.
Here is my code :
private void button6_Click(object sender, EventArgs e)
{
GhostscriptPngDevice devPNG = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png256);
devPNG.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
devPNG.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
devPNG.ResolutionXY = new GhostscriptImageDeviceResolution(96, 96);
devPNG.InputFiles.Add(#"D:\Public\FOS.pdf");
devPNG.OutputPath = #"D:\Public\FOS.png";
devPNG.Process();
}
I tried replacing the path of the input and output by one without any space and it now works ! Here is the code I ended up using :
using Ghostscript.NET.Rasterizer;
private void button6_Click(object sender, EventArgs e)
{
int desired_x_dpi = 96;
int desired_y_dpi = 96;
string inputPdfPath = #"D:\Public\temp\rasterizer\FOS.pdf";
string outputPath = #"D:\Public\temp\rasterizer\output\";
using (var rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(inputPdfPath);
for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
var pageFilePath = Path.Combine(outputPath, string.Format("Page-{0}.png", pageNumber));
var img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
img.Save(pageFilePath + "ImageFormat.Png");
}
}
}
Try to replace any character strange (non alpanumeric and spaces, keep filepath "clean" and in shared/temp folder to grant access to any user/process and should work just fine
Related
I wrote a small code to make a slideshow program on a different screen. Everything is running fine also the delete and copy part, unless I do it right after the first picture is shown. If I do it later the debugger steps in.
private void timer1_Tick(object sender, EventArgs e)
{
string[] images = Directory.GetFiles(#"D:\reflexscherm\root\Logo-teams2", "*.*");
counter++;
var maxcount = images.Count();
textBox1.Text = maxcount.ToString();
if (counter > maxcount - 1)
{
counter = 0;
maxcount = images.Count();
}
//pb1.Image.Dispose();
pb1.Image = Image.FromFile(images[counter]);
//Image oldImage = pb1.Image;
//pb1.Image.Dispose();
//oldImage.Dispose();
//pb1.Image = Image.FromFile(images[counter]);
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
Image oldImage = pb1.Image;
pb1.Image = Image.FromFile(#"D:\reflexscherm\root\sponsor1\x. Groot-logo-REFLEX.jpg");
pb1.Image.Dispose();
oldImage.Dispose();
string[] files = System.IO.Directory.GetFiles(sourcepath);
string[] delfiles = Directory.GetFiles(targetpath);
this.Hide();
foreach (string d in delfiles)
{
Image oldI = pb1.Image;
pb1.Image = Image.FromFile(#"D:\reflexscherm\root\sponsor1\x. Groot-logo-REFLEX.jpg");
//pb1.Image.Dispose();
oldI.Dispose();
File.Delete(d);
}
foreach (string s in files)
{
string fname = s.Substring(sourcepath.Length + 1);
File.Copy(Path.Combine(sourcepath, fname), Path.Combine(targetpath, fname), true);
this.Show();
timer1.Start();
}
What I am looking for is some help to adjust the code, so when I change files in sourcefolder then program copies the files from the sourcefolder to the targetfolder. I know how to use filewatcher. I am using a button to test the code.
Method 1: Preserve a copy and assign a Control's property
Use this method when a Bitmap object is handled in more than one place, so we need to preserve it for further elaborations/assignments and save to disc after.
Assign, store and dispose of the source Bitmap immediately, deleting the Image file:
Bitmap bitmap = null;
//---------------------------------------------
string imagePath = #"[Path of the Image]";
bitmap?.Dispose();
pictureBox1.Image?.Dispose();
using (Bitmap tempImage = new Bitmap(imagePath, true))
{
bitmap = new Bitmap(tempImage);
pictureBox1.Image = bitmap;
}
File.Delete(imagePath);
Method 2: Assign the Bitmap and dispose of it immediately
This method can be used when you need to assign a Bitmap to a Control and then move/delete the Image file. The Image is disposed of immediately, so it's only available through a Control's property: if we ask to have it back, sometimes what we get is not exactly what we gave.
string imagePath = #"[Path of the Image]";
using (Image image = Image.FromFile(imagePath, true))
{
pictureBox1.Image?.Dispose();
pictureBox1.Image = new Bitmap(image);
}
File.Delete(imagePath);
Note that the old image assigned to the Control, if any, is disposed of before assigning a new one.
Also note I'm always specifying to preserve the internal ICM informations, if any, specifying true as the second parameter of both new Bitmap(imagePath, true) and Image.FromFile(imagePath, true).
Some images won't look as the original if we don't.
You should use a readonly File Access if you don't want it to be locked:
using( FileStream stream = new FileStream( path, FileMode.Open, FileAccess.Read ) )
{
image = Image.FromStream( stream );
}
Hope it helps...
I had the same problem before, i did the following:
this.photo.Dispose();
this.photo.Refresh();
this.photo.Image.Dispose();
this.photo.Image = null;
this.photo.ImageLocation = null;
and it worths it.
My code
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Clear();
var img = new Bitmap(openFileDialog1.FileName);
//var ocr = new TesseractEngine("./tessdata", "eng", EngineMode.TesseractAndCube);
var ocr = new TesseractEngine("./rus", "rus", EngineMode.TesseractAndCube);
var page = ocr.Process(img);
textBox1.Text = page.GetText();
}
}
Code works fine with English trained data, but it throws an error when I change it to Russian.
Here is the error:
Tesseract.TesseractException: "Failed to initialise tesseract engine..
See https://github.com/charlesw/tesseract/wiki/Error-1 for details."
My Tesseract version is 3.0.2.
I've downloaded Russian tessdata files from https://github.com/tesseract-ocr/tesseract/wiki/Data-Files#data-files-for-version-302
work for me
Tesseract tesseract = new Tesseract();
tesseract.setLanguage("rus");
try {
tesseract.setDatapath("/home/test/tessdata");
String text = tesseract.doOCR(new File("/home/test/Pictures/photo.jpg"));
System.out.print(text);
} catch (TesseractException e) {
e.printStackTrace();
}
test data - https://github.com/tesseract-ocr/tessdata
Confirmed that problem. Tesseract can run with single language (I've tried bul.traineddata). But "rus" always gives that result in logcat:
Could not initialize Tesseract API with language=rus!
Of cause I've had rus.traineddata file in assets :-)
I have asp:FileUpload and I want to save all uploaded images of all acceptable formats as png images in the Images forlder on website, my upload code is:
protected void btnSave_Click(object sender, EventArgs e)
{
if (fup2.HasFile)
{
Regex reg = new Regex(#"(?i).*\.(gif|jpe?g|png|tif)$");
string uFile = fup2.FileName;
if (reg.IsMatch(uFile))
{
string saveDir = Server.MapPath(#"~/Images/");
string SavePath = saveDir + uFile;
fup2.SaveAs(SavePath);
}
else
{
Response.Write("Error");
}
}
}
I also tried using
var tempImg = Image.FromFile(Server.MapPath(#"~/Images/"));
tempImg.Save("a.tiff", ImageFormat.png);
It keeps throwing file not found exception
Any new ideas?
Use Bitmap.FromStream. Something like:
using System.Drawing;
protected void btnSave_Click(object sender, EventArgs e)
{
if (fup2.HasFile)
{
Regex reg = new Regex(#"(?i).*\.(gif|jpe?g|png|tif)$");
string uFile = fup2.FileName;
if (reg.IsMatch(uFile))
{
string saveDir = Server.MapPath(#"~/Images/");
string SavePath = saveDir + Path.GetFileName(uFile) + ".png";
Bitmap b = (Bitmap)Bitmap.FromStream(fup2.PostedFile.InputStream);
b.Save(SavePath, ImageFormat.Png);
}
else
{
Response.Write("Error");
}
}
}
The Image.FromFile -> Save should do the trick, but i don't see how you are using the correct path - you just point at the directory and not the actual file when calling FromFile
As a side note, doing this processing on a web processing thread is not a great idea but for small load it could work.
My program run fine when i open the exe in output directory of C# Application but when i copied exe and exe.config file to Desktop and run it, program stops in the middle.
1- I am using Background worker.
2- I am reading a string from appconfig just after InitializeComponent(); in Form Constructor
3-Cross Thread Processing is not allowed. and i have checked there is no such exception.
4- i have added many dll's in reference.
Point where it stops executing is shown in the code in comments.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DrawImages(0, 222, 145);
// Line below executes and label is changed
//SetLabelText("Creating Movie Here");
CreateMovieFromImages();
.
.
.
}
and the code of CreateMovieFromImages() is
private void CreateMovieFromImages()
{
//Line below this point never execute and no message box displays nor any label is changed.
MessageBox.Show("OK");
try
{
SetLabelText("Creating Movie");
for (int i = 0; i < 50; i++)
{
ImagesUrls.Add(outputDirectory+ "//Tempe" + i + ".bmp");
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
int width = 1254;
int height = 1000;
var framRate = 5;
try
{
using (var vFWriter = new VideoFileWriter())
{
vFWriter.Open(outputDirectory + "//TemperaryVideo.wmv", width, height, framRate, VideoCodec.WMV2);
foreach (var i in ImagesUrls)
{
Bitmap bmp = new Bitmap(i);
SetLabelText("Writing Image" + i + "\n");
var bmpReduced = ReduceBitmap(bmp, width, height);
vFWriter.WriteVideoFrame(bmpReduced);
}
vFWriter.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
I have never tried to run exe like this before so,it is my First time . Maybe i need someother thing to run exe like this .
Exe works fine when it is in output directory of VS Solution.
Thanks a lot for reading my all Question .kindly help me :)
Copy the whole output directory, not just the executable. It depends on the other .dll files that you have added as a reference.
I am using the ADF Scanner library by Gideon (http://adfwia.codeplex.com/) and have hit a small problem. While I can scan a file it throws an exception when saving. I'll post the full code:
private void button1_Click(object sender, EventArgs e)
{
ADFScan _scanner;
int[] _colors = { 1, 2, 4 };
int count = 0;
_scanner = new ADFScan();
_scanner.Scanning += new EventHandler<WiaImageEventArgs>(_scanner_Scanning);
_scanner.ScanComplete += new EventHandler(_scanner_ScanComplete);
ScanColor selectedColor = ScanColor.Color;
// ScanColor selectedColor = (ScanColor)_colors[comboBox1.SelectedIndex];
int dpi = 300;
_scanner.BeginScan(selectedColor, dpi);
}
void _scanner_ScanComplete(object sender, EventArgs e)
{
MessageBox.Show("Scan Complete");
}
void _scanner_Scanning(object sender, WiaImageEventArgs e)
{//e.ScannedImage is a System.Drawing.Image
int count = 0;
string filename = "C:\\test.jpg";
e.ScannedImage.Save(filename, ImageFormat.Jpeg);//FILES ARE RETURNED AS BITMAPS
}
The program begins to scan without an issue, and in fact can scan multiple pages at the same time (what I needed!) The exception thrown is this one
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
Huh? Anyone with experience with this can help me? Thanks a lot in advance =)