Error : A generic error occurred in GDI+. while saving Image - c#

While i am trying to save Image getting this error
A generic error occurred in GDI+
I searched for this error and checked write permissions for this folder ,and also checked that image file is not in use by anything else (including my code) But While I am trying to save Image still getting same error,Where is the mistake
public partial class AdsMaster : Form
{
OpenFileDialog ofd=null;
public AdsMaster()
{
InitializeComponent();
}
private void browseButton_Click(object sender, EventArgs e)
{
ofd = new OpenFileDialog();
ofd.Filter = "image files|*.jpg;*.jpeg;*.gif;*.png;*.bmp";
DialogResult dr = new DialogResult();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName);//create the bitmap
string imgName = ofd.SafeFileName;
txtImagePath.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
img.Dispose();
}
}
private void saveImage_Click(object sender, EventArgs e)
{
String str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = str + "\\Image\\";
Image img = new Bitmap(ofd.FileName);
string imgName = ofd.SafeFileName;
try
{
img.Save(path + imgName); //getting error at this line
MessageBox.Show("Image is saved");
img.Dispose(); // dispose of your image
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
}

I think the problem here may be due to the fact that a lock is placed on the original image when you call
Image img = new Bitmap(ofd.FileName);
The following should do the trick
private void saveImage_Click(object sender, EventArgs e)
{
string str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = str + "\\Image\\";
string imgName = ofd.SafeFileName;
try
{
File.Copy(ofd.FileName, path + imgName);
MessageBox.Show("Image is saved");
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}

I resolve this error by changing the file name and path to where the file is stored.
You needs to check either one of them-
1- FileName should be different/ should not get matched with other files name which have been already present on that location where new file/image you want to store.
2- change the (save ) location of the new file from operating system drive.
try to avoid to store the new file/Image on a drive on which operating system has been already installed.
Below code works for me
IWebdriver driver;
driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32");
driver.Navigate().GoToUrl("http://www.gmail.com");
driver.Manage().Window.Maximize();
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);

Related

How can I save a User's uploaded image to a temp file that I can access later, but gets deleted after that

I've created an image uploader for users to upload/crop their profile pics. When a user uploads a file, it gets saved to a file that gets accessed immediately after for them to crop it.
I've tried using Path.GetTempPath() as well as Path.GetTempFileName(), but my cropper was unable to locate the file locations for some reason.
Any suggestions are greatly appreciated.
UPDATE: So I added File.Delete(filePath) to the end of btnCropClick, but I get an error saying The process cannot access the file because it is being used by another process. How can I "release" that file to be deleted instantly ?
Below is the code for where a user uploads their orignal image of choice
protected void btnUploadClick(object sender, EventArgs e)
{
//Upload Original Image Here
String UploadFileName = "";
String UploadFilePath = "";
if (fileUploader.HasFile)
{
String ext = Path.GetExtension(fileUploader.FileName).ToLower();
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png")
{
UploadFileName = "orig_" + Guid.NewGuid().ToString() + ext;
UploadFilePath = Path.Combine(Server.MapPath("images/OriginalImages"), UploadFileName);
try
{
fileUploader.SaveAs(UploadFilePath); //TODO: Need to make this a temp file that gets "destroyed" later
imgUpload.ImageUrl = "images/OriginalImages/" + UploadFileName;
panCrop.Visible = true;
}
catch (Exception ex)
{
lblMsg.Text = "Error! Please Try Again. ";
}
}
else
{
lblMsg.Text = "Invalid File Type Selected. | Please Choose .jpg, .jpeg, or .png file only.";
}
}
else
{
lblMsg.Text = "Please Click 'Choose File' & Select An Image To Upload";
}
}
And here is the code for the cropper (Not sure anything in here needs to be changed, but I'll include it anyway for context & relevancy
protected void btnCropClick(object sender, EventArgs e)
{
//Crop Image Here & Save
String fileName = Path.GetFileName(imgUpload.ImageUrl);
String filePath = Path.Combine(Server.MapPath("images/OriginalImages"), fileName);
String cropFileName = "";
String cropFilePath = "";
if (File.Exists(filePath))
{
System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);
Rectangle CropArea = new Rectangle(
Convert.ToInt32(X.Value),
Convert.ToInt32(Y.Value),
Convert.ToInt32(W.Value),
Convert.ToInt32(H.Value)
);
try
{
Bitmap bitMap = new Bitmap(CropArea.Width, CropArea.Height);
using (Graphics g = Graphics.FromImage(bitMap))
{
g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), CropArea, GraphicsUnit.Pixel);
Bitmap resized = new Bitmap(bitMap, new Size(200, 200)); //Resize image to save as 200x200
cropFileName = "crop_" + fileName; //+UserID so each fileName is unique
cropFilePath = Path.Combine(Server.MapPath("images/CroppedImages"), cropFileName);
resized.Save(cropFilePath); //Where final, cropped image is saved
imgHeadshot.ImageUrl = "images/CroppedImages/" + cropFileName;
}
}
catch (Exception ex)
{
throw;
}
panCrop.Visible = false;
}
}
Was able to dispose of the original file by adding:
orgImg.Dispose();
bitMap.Dispose();
File.Delete(filePath);
to the end of btnCropClick method.

Using Tesseract in C#

I have tesseract installed and I am using button click to set location of tesseract.exe file. I am also using another button click to set the location of the image file. Now I want the third button click to process the image with tesseract as I have stored their respective locations.
I am using some basic crude approach but it suits me.
My code is like:
private void B8_Click(object sender, EventArgs e)
{
q = z + "\\" + "output.txt";
if (k != null)
{
Process pr = new Process();
pr.StartInfo.FileName = j;
pr.StartInfo.Arguments = k + " " + q;
pr.Start();
pr.WaitForExit();
}
else
{
MessageBox.Show("No Image File Selected.");
}
var filetext = File.ReadAllText(q);
tb5.Text = filetext;
//File.Delete(q);
}
private void B10_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
j = "\"" + openFileDialog1.FileName + "\"";
MessageBox.Show("Tesseract Location Set: " + j);
}
}
private void B9_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
k = "\"" + openFileDialog1.FileName + "\"";
MessageBox.Show("Image File Location Set: " + k);
}
}
My 3-button click story so far:
I have successfully run the code with 1-button to set the tesseract.exe path, 2-button to set the image path, but the 3-button (see B-8) has an issue.
It extracts the text and stores into an "output.txt" file. But, I am not able to import this text into my textbox tb5 and then destroy this file.
The error I am getting is Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Could not find file 'C:\Users\ambij\Desktop\try\output.txt'.
I don't understand this but there is actually output.txt file residing in the folder.
The following is for Tesseract 3.05.02 - May work in a later version
private void RunIt()
{
string tessDataPath = yourTessDataPath; // Your Tesseract Location Set
string imagePath = yourImagePath; // The Image File Location
string theTextFromTheImage = DoOCR(yourTessDataPath, yourImagePath);
// Some formatting may be required - OCR isn't perfect
MessageBox.Show(theTextFromTheImage);
}
private string DoOCR(string tessdataPath, string filePath)
{
string returnText = "";
using (var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default))
{
// engine.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); // Only regular letters
string theVersion = engine.Version; // Optional, but useful
using (var img = Pix.LoadFromFile(filePath))
{
using (var page = engine.Process(img))
{
returnText = page.GetText();
}
}
}
return returnText;
}

Issue with output location when saving file

Currently having an issue when saving a merged word. doc to a specific location using a filebrowser dialog
// input destintion
private string[] sourceFiles;
private void browseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog diagBrowser = new FolderBrowserDialog();
diagBrowser.Description = "Select a folder which contains files needing combined...";
// Default folder, altered when the user selects folder of choice
string selectedFolder = #"";
diagBrowser.SelectedPath = selectedFolder;
// initial file path display
folderPath.Text = diagBrowser.SelectedPath;
if (DialogResult.OK == diagBrowser.ShowDialog())
{
// Grab the folder that was chosen
selectedFolder = diagBrowser.SelectedPath;
folderPath.Text = diagBrowser.SelectedPath;
sourceFiles = Directory.GetFiles(selectedFolder, "*.doc");
}
}
// output destintion
private string[] sourceFileOutput;
private void browseButtonOut_Click(object sender, EventArgs e)
{
FolderBrowserDialog diagBrowserOutput = new FolderBrowserDialog();
diagBrowserOutput.Description = "Select a folder location to save the document...";
// Default folder, altered when the user selects folder of choice
string outputFolder = #"";
diagBrowserOutput.SelectedPath = outputFolder;
// output file path display
outputPath.Text = diagBrowserOutput.SelectedPath;
if (DialogResult.OK == diagBrowserOutput.ShowDialog())
{
outputFolder = diagBrowserOutput.SelectedPath;
outputPath.Text = diagBrowserOutput.SelectedPath;
sourceFileOutput = Directory.GetFiles(outputFolder);
}
}
private void combineButton_Click(object sender, EventArgs e)
{
if (sourceFiles != null && sourceFiles.Length > 0)
{
string outputFileName = (sourceFileOutput + "Combined.docx");
MsWord.Merge(sourceFiles, outputFileName, true);
// Message displaying how many files are combined.
MessageBox.Show("A total of " + sourceFiles.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// Message displaying error.
MessageBox.Show("Please a select a relevant folder with documents to combine", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
instead of getting the 'combined.docx' in the location chosen, i instead get a file called 'System.String[]Combined' saved on the desktop. Obviously there is something clashing regarding the name and the user selected file path.
i currently have the input folder options working however the output + file name doesn't seem to be working correctly.
any suggestions or help would be greatly appreciated, thank you.
string outputFileName = (sourceFileOutput + "Combined.docx");
This should probably read
string outputFileName = selectedFolder + "Combined.docx";
That said, please use Path.Combine to combine two parts of a path.
got the program to use the 'selected' destination.
// output destintion
string outputFolder = #"";
private void browseButtonOut_Click(object sender, EventArgs e)
{
FolderBrowserDialog diagBrowserOutput = new FolderBrowserDialog();
diagBrowserOutput.Description = "Select a folder location to save the document...";
// Default folder, altered when the user selects folder of choice
diagBrowserOutput.SelectedPath = outputFolder;
// output file path display
outputPath.Text = diagBrowserOutput.SelectedPath;
if (DialogResult.OK == diagBrowserOutput.ShowDialog())
{
outputFolder = diagBrowserOutput.SelectedPath;
outputPath.Text = diagBrowserOutput.SelectedPath;
}
}
private void combineButton_Click(object sender, EventArgs e)
{
if (sourceFiles != null && sourceFiles.Length > 0)
{
string folderFolder = outputFolder;
string outputFile = "Combined.docx";
string outputFileName = Path.Combine(folderFolder, outputFile);
MsWord.Merge(sourceFiles, outputFileName, true);
// Message displaying how many files are combined.
MessageBox.Show("A total of " + sourceFiles.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
i used the path.combine as suggested, as played around with the variables i had used.

How to save image jpg or other format after uploading as png using asp.net c#

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.

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();

Categories

Resources