|
I didn't know this until recently, when I did it by accident - imagine the chances of that happening !
|
|
|
|
|
cool
|
|
|
|
|
I agree, thanks for the tip!
|
|
|
|
|
I had no idea! Real cool! -Bryan
My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
You may want to check out GhostDoc[^]
It is a free add on for Visual Studio that makes documenting stuff (for Intellisense or not) realy easy.
Ctl+Shift+d while the insertion point is in your enum and all of the ///<summary></summary> tags for each option are inserted (and the stuff at the top).
I am a bit of a fan since I found it.
|
|
|
|
|
Putting all enums in a switch statement simply because they exist is pointless. There are MANY times when you simply don't care about 1 or more of the ordinals (lumping their handling into the default: clause)..45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
i am trying to write a title to an image and save it back to its current location but i am having a problem as the following message is being thrown:
A generic error occured in GDI+
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Drawing
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Drawing
Error code: -2147467259
" at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n
at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges()
in E:\\ZooSystem\\\ImageViewer.cs:line 520"
I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions but the folder has "Read-Only" enabled.
The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode.
If i save the images to another folder say one of the Public folders then the above exception is not thrown.
But I would like to save my images to the same folder if possible?
How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours.
Thanks,
Here is the code i am using:
private void ApplyChanges()
{
if(txtBoxOut.Text.Equals("")
{
if(sfd.ShowDialog()==DialogResult.OK)
{
this.SaveTheImage(sfd.FileName,this.bmp);
}
}
else if(txtBoxOut.Text != "")
{
string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
this.bmp = Image.FromFile(filePath);
string fileName = Path.GetFileName(filePath);
try
{
if(destFolder.EndsWith("\\")
{
this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg);
}
else
{
this.bmp.Save(destFolder+"\\"+fileName+ImageFormat.Jpeg);
}
}
catch(Exception e)
{
Console.WriteLine(e.message.ToString());
}
}
}
}
|
|
|
|
|
Most, if not all, errors inside GDI+ are reported as "generic problem occurred in GDI+". If the affected line is an Image.Save chances are your path is incorrect or inaccessible, your disk is full, or your destination file exists and is locked.
If you load an image from a file, most of the time the file remains locked as long as the Image is alive. This would prevent you from saving an image to the same path.
It applies to Image.FromFile, and probably also to PictureBox.ImageLocation
The one exception I am aware of is when you use Image.FromStream.
So I suggest one of these two workarounds:
Bitmap bm=null;
{
Bitmap bm1=Image.FromFile(filepath);
bm=new Bitmap(bm1);
bm1.Dispose();
}
or
Bitmap bm=null;
using (FileStream stream=File.OpenRead("image.jpeg")) {
bm=Image.FromStream(stream);
}
|
|
|
|
|
According to Documentation[^]
"You must keep the stream open for the lifetime of the Image." so should not the second snippet throw an exception?
|
|
|
|
|
Hi Giorgi,
Thanks. I missed that line in the documentation, and no it has never thrown me an Exception; AFAIK once the pixels are loaded, file (and now also the stream) access is needed only when extra information is asked for, (e.g.the JPEG tags), which I don't ask for unless I use Image.FromFile().
Theoretically I now must change my standard reply to always create a copy of the first image, then dispose of it. Grr.
|
|
|
|
|
Windows probably has the bitmap locked if you are saving the file over itself.
If you are trying to save the image over itself. First, create a copy of the image and dispose of the original bitmap. Then apply the title to the copy and save it over the original image. Once disposed, it should not give you any problems.
you can use the Bitmap(Bitmap) constructor in order to make a copy.-Bryan
My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
Hi,
Thanks for the advice.
I have checked the amount of space on my HDD and i have 255 GB free. I am trying to save the images back to a folder which is located in "Pictures" folder.
I tried to make a copy of the image but it still throws an exception. My write method writes the title to the global image after i have created the image.
I am using Filestream as advised. I have wrapped the whole block of code when using the "using" directive.
I also dispose the global image after i have made a copy of it. I used the clone method and then use the cloned image to be saved. But i still get the same error message.
Any thoughts?
Here is a segment of the code:
else if(txtBoxOut.Text != "")
{
string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);
try
{
Image newImage = (Image)this.bmp.Clone();
this.bmp.Dispose();
if(destFolder.EndsWith("\\")
{
this.newImage.Save(destFolder+fileName+ImageFormat.Jpeg);
}
else
{
this.newImage.Save(destFolder+"\\"+fileName+ImageFormat.Jpeg);
}
}
catch(Exception e)
{
Console.WriteLine(e.message.ToString());
}
}
}
}
|
|
|
|
|
Hi,
In this bit of code you still have the stream open because you are doing your bitmap operations inside your using. Windows may still have hold of the bitmap. Copy the bitmap inside the using and do the other stuff outside of the using. See if that works. something like this...
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);
Image newImage = (Image)this.bmp.Clone();
this.bmp.Dispose();
}
if(destFolder.EndsWith("\\"))
.
.
. -Bryan
My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
Hi,
I tried to do the above but an exception thrown when i try to write the title. The newImage is global in my case because the title has to be written this way...
I try to check outside the using block if the contents in the textbox has been changed and if it has i call the ApplyTitle method which is meant to write the title. But now it throws the exception when i try to write the title.
The exception is also thrown again when i try to save the cloned image
|
|
|
|
|
when you want to create a new file, unrelated to existing, already open, files all stuff about locked files, file streams, etc is irrelevant. What is relevant though is:
destFolder+fileName+ImageFormat.Jpeg
is non-sense. Store that in a string variable and look at its value.
|
|
|
|
|
Yes, Luc is right... Wow, i don't believe i didn't see that.
-Bryan
My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
Hi,
Thanks for your assistance,
Just looked at this post after i posted my previous post.
Duh me, I can see that its writing jpeg twice. I have changed this so its like C:\.......\image.jpg
But
I tried to save the file and I still get the same exception. By the way I have done what Bryan advised me to do.
I am going to revert my changes i have made apart from the change i have made to the save method.
*Update*
I have reverted the changes and i still get the same exception. I have checked my ApplyTitle method and all it is doing is setting the user specified value of the title propertyitem of the image.
Any further advice would be much appreciated.
Thanksmodified on Friday, March 5, 2010 7:21 PM
|
|
|
|
|
Eagle32 wrote: Console.WriteLine(e.message.ToString());
No
Not like that. Message is already a string, calling ToString() on it doesn't do anything but waste CPU cycles. The net result is still a single line of text.
What you should do is e.ToString() that will provide all available information. Do it now, and tell us what it gives you.
|
|
|
|
|
Hi,
Thanks, Here is what i get:
System.Runtime.InteropServices.ExternalException was caught
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ZooSystem.ImageViewer.ApplyChanges() in E:\ZooSystem\ImageViewer.cs:line 520
InnerException:
I suspect it is something to do with folder permissions but i have checked the folder and file permissions.
I am saving the animal pictures back to its original location which is C:\Users\<username>\Pictures\Animals
Now in the code it looks like this C:\\Users\\<username>\\Pictures\\Animals.
|
|
|
|
|
Just an odd question. What happens when you try to save the image in the same folder but a different name?
Also, what happens if you go to this folder in Windows explorer and try to create a file? like a new text document or the like.
Does UAC gripe at you or something?-Bryan
My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
Apologies for late reply, it appears to be working.
Thanks for the help.
|
|
|
|
|
Your welcome. -Bryan
My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
I have a button on a form that triggers a pop up (This is a Windows Form App, not an ASP.Net App) to collect additional data based on what the related combobox value was when the button was pressed. This form can have anywhere from 2 to 10 inputs (Radio Buttons, Combo Boxes) on the screen.
The issue is this: I don't want to make 15 forms to accommodate all of the possibilities, so I decided (Or am still deciding) to make a dynamic form that accepts the combobox value as an argument. So parent form has combobox A and sends that value to child form on button press.
First of all I would like to know what you think of the idea of having a dynamic form vs. 15 forms.
Secondly, on the dynamic form idea I decided to create a form with all of the possible elements (5 groupboxes with radio buttons, 5 comboboxes, 1 multiline textbox and 1 save button) and then resize and show / hide / position the controls as necessary based on the input. I can handle this well enough, though it seems laborious, but I am having an issue with dynamically loading information into my comboboxes, see this code:
switch (theId)
{
case 24:
this.Size = new System.Drawing.Size(348, 619);
foreach (Control ctrl in Controls)
{
ctrl.Visible = true;
}
break;
case 25:
string[] myControls = new string[4] { "groupBox1", "label1", "comboBox1", "btnSave" };
foreach (Control ctrl in Controls)
{
if (Array.IndexOf(myControls, ctrl.Name) == -1)
{
ctrl.Visible = false;
}
else
{
ctrl.Visible = true;
switch (Array.IndexOf(myControls, ctrl.Name))
{
case 0:
ctrl.Text = "Blah";
break;
case 1:
ctrl.Text = "Foo";
break;
case 2:
if (ctrl is ComboBox)
{
My breakdown is here at the end, the Control ctrl won't accept the Combobox.Items.Add, so how do I add items to it? And is this whole approach just not right?
|
|
|
|
|
Well, you didn't show the line where you are calling ComboBox.Items.Add
But have you tried this?
try
{
(ctrl as ComboBox).Items.Add(whatevertoaddhere);
}
catch(Exception ex)
{
}
or something of the like.My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
|
|
|
|
|
It's hard to tell where your 15 choices come from, but I recently had a similar problem. If you're choices are based on a reasonably designed class or classes, you should look at using reflection to dynamically add the controls you need:
foreach (Type mytype in Assembly.GetEntryAssembly().GetTypes())
{
if (type_matches_criteria)
{
(create new control)
(adjust control details and location)
Controls.Add(new_control)
}
}
Then to use your controls:
foreach (Control ctrl in (from Control c in Controls where c (meets criteria) select c))
{
if (ctrl.Checked)
{
ctrl.Text = "Blah";
}
}
|
|
|
|