I thought I am writing 10 file extensions and their related Icons as Bitmap into a resource file within a for loop. The odd thing is that only the last file extension with its Icon is written into the Resource.resx file. Somehow the next file extension in the loop is overwriting the previous one, but WHY ? I thought a resource is sort of a dictionary with key/value pair where I can add as much as I want just as I do in the Resource designer...
What do I wrong?
My code:
private void AddDocument()
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
DialogResult result = fileDialog.ShowDialog();
if (result == DialogResult.OK)
{
for (int i = 0; i < fileDialog.FileNames.Length; i++)
{
string absoluteFilePath = fileDialog.FileNames.GetValue(i).ToString();
byte[] file = File.ReadAllBytes(absoluteFilePath);
String fileExtension = Path.GetExtension(absoluteFilePath);
Bitmap gdiImage;
Document doc = new Document();
doc.DocumentData = file;
doc.DocumentName = fileDialog.SafeFileNames.GetValue(i).ToString();
if (TryIsFileExtensionExisting(fileExtension, out gdiImage))
{
// Filetype was saved before => Convert GDI Bitmap to wpf BitmapImage
doc.DocumentTypeImage = gdiImage.ConvertGDIImageToWPFBitmapImage();
}
else
{
BitmapImage wpfImage;
// Filetype is new => get Bitmap out of the Icon
Icon icon = IconFromFilePath(absoluteFilePath);
Bitmap bitmap = icon.ToBitmap();
wpfImage = bitmap.ConvertGDIImageToWPFBitmapImage();
doc.DocumentTypeImage = wpfImage;
// Save bitmap to resource
using (ResXResourceWriter writer = new ResXResourceWriter("TBM.Resource"))
{
writer.AddResource(fileExtension, bitmap);
writer.Generate();
}
}
DocumentList.Add(doc);
}
_documentService.AddDocumentsToPeriod(DocumentList, _parentId);
}
}
private bool TryIsFileExtensionExisting(String fileExtension, out Bitmap wpfImage)
{
DictionaryEntry entry;
using (ResXResourceReader reader = new ResXResourceReader ("TBM.Resource"))
{
entry = reader.Cast<DictionaryEntry>()
.Where(x => x.Key.ToString()
.Equals(fileExtension, StringComparison.CurrentCultureIgnoreCase))
.FirstOrDefault();
};
wpfImage = entry.Value as Bitmap;
return entry.Key != null;
}
private Icon IconFromFilePath(string filePath)
{
Icon result = null;
try
{
result = Icon.ExtractAssociatedIcon(filePath);
//'# swallow and return nothing. You could supply a default Icon here as well
}
catch
{
}
return result;
}
The problem is here:
using (ResXResourceWriter writer = new ResXResourceWriter("TBM.Resource"))
{
writer.AddResource(fileExtension, bitmap);
writer.Generate();
}
Each time you create a new writer object and write to it. But you don't have the creation of the writer object read from the old file. So you overwrite every time. You should be able to use a different constructor and solve your problem.
http://msdn.microsoft.com/en-us/library/system.resources.resxresourcewriter.aspx
Related
this is a C# winforms project
Given a file folder, need to open any pdf file and for each file,
evaluate every object. If the object is an image, extract it, convert it and save it
as a Jpeg file in that same folder.
private void btnExtractImages_Click(object sender, EventArgs e)
{
try
{
DirectoryInfo di = new DirectoryInfo(txtSourcePdf.Text);
foreach (FileInfo fi in di.GetFiles())
{
PdfDocument doc = new PdfDocument(new PdfReader(fi.FullName));
string fNm = fi.Name.Replace(".pdf", ""); //Filename without the extension
int pgs = doc.GetNumberOfPages();
for (int pg = 1; pg <= pgs; pg++)
{
PdfPage pdfPage = doc.GetPage(pg);
PdfFormXObject xObj = pdfPage.CopyAsFormXObject(doc);
iText.Layout.Element.Image img = new iText.Layout.Element.Image(xObj);
//good so far.
//need to turn this xObj or img into a jpg and save to disk
...
}
// This works, however it creates a .dat file, not a .jpg file
// int objs = doc.GetNumberOfPdfObjects();
// for (int o = 1; o <= objs; o++)
// {
// PdfObject obj = doc.GetPdfObject(o);
// if (obj != null && obj.IsStream())
// {
// byte[] b;
// try
// {
// // Get decoded stream bytes.
// b = ((PdfStream)obj).GetBytes();
// }
// catch (Exception)
// {
// //Get originally encoded stream bytes
// b = ((PdfStream)obj).GetBytes(false);
// }
// using (FileStream fos = new FileStream(String.Format(txtSourcePdf.Text + "/" + fNm + "{0}.jpg", o), FileMode.Create))
// {
// fos.Write(b, 0, b.Length);
// }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
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);
}
Below is some code that
creates a directory
uses MagickNet to covert a PDF into separate BMP images and stores it in a folder (ImagePath)
Then it uses TessNet2 to scan each image in that folder to parse out information
I can't seem to get the foreach loop that scans the ImagePath folder. Any help?
The error right now is on the 'foreach' statement and it says "Cannot convert type 'char' to 'System.Drawing.Image'"
static void Main(string[] args)
{
string ImagePath = exePath + "\\Images";
if (!Directory.Exists(ImagePath))
{
Directory.CreateDirectory(ImagePath);
}
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(300, 300);
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(#"D:\Test\ABStest.pdf",settings);
int page = 1;
foreach (MagickImage image in images)
{
image.Write(ImagePath + "\\ABS" + page + ".bmp");
page++;
}
}
foreach (Image item in ImagePath)
{
using (Bitmap bmp = new Bitmap(item))
{
tessnet2.Tesseract tessocr = new tessnet2.Tesseract();
tessocr.Init(#"C:\Users\Matt Taylor\Documents\Visual Studio 2012\Projects\TessNet2\TessNet2\bin\Debug\tessdata", "eng", false);
tessocr.GetThresholdedImage(bmp, Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".bmp");
// Tessdata directory must be in the directory than this exe
Console.WriteLine("Multithread version");
ocr.DoOCRMultiThred(bmp, "eng");
//Console.WriteLine("Normal version");
//ocr.DoOCRNormal(bmp, "eng");
}
}
}
You can use Directory.GetFiles to return all the filenames in the directory and create your Bitmaps from there
foreach (string imageFileName in Directory.GetFiles(ImagePath))
{
using (Bitmap bmp = new Bitmap(imageFileName))
{
}
}
But if there are other files in that folder you should add a filter
foreach (string imageFileName in Directory.GetFiles(ImagePath, "*.jpg"))
{
using (Bitmap bmp = new Bitmap(imageFileName))
{
}
}
You don't need to save the file to disk. You could use the .ToBitmap() method of MagickImage.
foreach (MagickImage image in images)
{
using (Bitmap bmp = image.ToBitmap())
{
tessnet2.Tesseract tessocr = new tessnet2.Tesseract();
// etc...
}
}
You are looping through String ImagePath, which gives you a collection of characters. You need to search through the directory with Directory.GetFiles(), and load the images with Image.FromFile():
foreach (String itemPath in Directory.GetFiles(ImagePath))
{
using (Image item = Image.FromFile(itemPath))
{
...
}
}
I want to upload an image file and then extract its basic information (author, dimensions, date created, modified, etc) and display it to the user. How can I do it.
A solution or reference to this problem in asp.net c# code would be helpful. But javascript or php would be ok as well.
Check this Link. You will get more Clearance about GetDetailsOf() and its File Properties based on the Win-OS version wise.
If you want to use C# code use below code to get Metadata's:
List<string> arrHeaders = new List<string>();
Shell shell = new ShellClass();
Folder rFolder = shell.NameSpace(_rootPath);
FolderItem rFiles = rFolder.ParseName(filename);
for (int i = 0; i < short.MaxValue; i++)
{
string value = rFolder.GetDetailsOf(rFiles, i).Trim();
arrHeaders.Add(value);
}
C# solution could be found here:
Link1
Link2
Bitmap image = new Bitmap(fileName);
PropertyItem[] propItems = image.PropertyItems;
foreach (PropertyItem item in propItems)
{
Console.WriteLine("iD: 0x" + item.Id.ToString("x"));
}
MSDN Reference
C# Tutorial Reference
try this...
private string doUpload()
{
// Initialize variables
string sSavePath;
sSavePath = "images/";
// Check file size (mustn’t be 0)
HttpPostedFile myFile = FileUpload1.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
//**************
//lblOutput.Text = "No file was uploaded.";
return null;
}
// Check file extension (must be JPG)
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
//**************
//lblOutput.Text = "The file must have an extension of JPG";
return null;
}
// Read file into a data stream
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
// Make sure a duplicate file doesn’t exist. If it does, keep on appending an
// incremental numeric until it is unique
string sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + ".jpg";
}
// Save the stream to disk
System.IO.FileStream newFile
= new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
return sFilename;
}
Solved
I figured out that the GetNewFolderNameBasedOnDate method internally didn't close the file. I have that method fixed and it working normal now
I am trying to move selected files from one folder to another using BackgroundWorker process in C#. Here is my DoWork() method that determine whether to move the files or just copy. My File.Move() throws an exception that "The process cannot access the file because it is being used by another process". I tried different methods as mentioned in the threads here in stackoverflow.
private void FileProcessor_DoWork(object sender, DoWorkEventArgs e)
{
// Copy files
long bytes = 0;
string destSubFolder = String.Empty;
string destFile = string.Empty;
foreach (FileInfo file in oSettings.SourceFiles)
{
try
{
this.BeginInvoke(OnChange, new object[] { new UIProgress(file.Name, bytes, oSettings.MaxBytes) });
destSubFolder = GetNewFolderNameBasedOnDate(file);
//Create a new subfolder under the current active folder
string newPath = Path.Combine(oSettings.TargetFolder, destSubFolder);
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(newPath))
{
System.IO.Directory.CreateDirectory(newPath);
}
destFile = Path.Combine(oSettings.TargetFolder, destSubFolder, file.Name);
if (chkDeleteSourceFiles.Checked)
{
FileInfo f = new FileInfo(file.FullName);
if (f.Exists)
{
File.Move(file.FullName, destFile);
}
}
else
{
File.Copy(file.FullName, destFile, true);
}
//Thread.Sleep(2000);
}
catch (Exception ex)
{
UIError err = new UIError(ex, file.FullName);
this.Invoke(OnError, new object[] { err });
if (err.result == DialogResult.Cancel) break;
}
bytes += file.Length;
}
}
I tried to delete the files in "RunWorkerCompleted" method too. But didn't resolve the problem. This fails when it tries to delete the last file in the list.
private void FileProcessor_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Operation completed, update UI
ChangeUI(false);
foreach (FileInfo file in oSettings.SourceFiles)
{
File.Delete(file.FullName);
}
}
GetNewFolderNameBasedOnDate() calls GetDateTaken() which was the culprit. Earlier I didn't use FileStream object but used Image myImage = Image.FromFile(filename); I didn't know that Image.FromFile locks the file.
private DateTime GetDateTaken(string fileName)
{
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
Image myImage = Image.FromStream(fs);
PropertyItem propItem = myImage.GetPropertyItem(36867);
DateTime dtaken;
//Convert date taken metadata to a DateTime object
string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
string firsthalf = sdate.Substring(0, 10);
firsthalf = firsthalf.Replace(":", "-");
sdate = firsthalf + secondhalf;
dtaken = DateTime.Parse(sdate);
return dtaken;
}
}
catch (Exception ex)
{
return DateTime.Now;
}
}
Instead of creating new FileInfo objects, keep it simple and re-use the same one. I suspect the problem is that you have multiple references to the same file in your code, which prevents it from being removed. Try something like this to move it:
if (chkDeleteSourceFiles.Checked)
{
if (file.Exists)
{
file.MoveTo(destFile);
}
}
My guess is that it is the BeginInvoke call to OnChange and the new UIProgress() object that is holding onto the file. Does UIProgress open the file? You could try just using Invoke() and see if that helps.