|
Well, this one has me stumped.
VS2008 Targetting .NET 3.5 (error also occurs when targetting .NET 2.0)
Given the following code:
private void OnBrowse(object sender, EventArgs e)
{
var fd = new OpenFileDialog
{
Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*",
Multiselect = false
};
if (fd.ShowDialog() == DialogResult.OK)
pictureBox1.Load(fd.FileName);
}
in a very simple WinForms app. The code loads and displays an image in the picture box control. So I know that part is working.
The following code:
private void OnSave(object sender, EventArgs e)
{
try
{
pictureBox1.Image.Save(@"c:\src\test\image.bmp", ImageFormat.Bmp);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
throws an 'A generic error occurred in GDI+' exception. The image file loaded in the OnBrowse handler is not the same image file I'm trying to save to. The target directory has read and write permissions. The output file is created when I run the program but has a length of zero bytes.
I've searched Google and all the solutions indicate a permissions error, which would make sense if the file isn't even created but, as it is created, I can't see it being a permission problem.
Any hints would be gratefully received.
modified on Sunday, July 27, 2008 5:55 AM
|
|
|
|
|
Hi,
this is my standard advice on Image.Save problems:
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
An alternative work-around is to work with a copy of the image:
load the image with Image.FromFile,
create a new image from it with new Bitmap(Image),
dispose of the original image.
Hope this helps.
|
|
|
|
|
Thanks for the advice Luc. Obviously the image in the pictureBox is locked because I changed the code to load the image into a new Bitmap object and save from there and it's now working.
|
|
|
|
|
You're welcome.
|
|
|
|
|
I'm sure there's lots out there. Maybe you folks have your favorites?
|
|
|
|
|
Have one, but in C++. Would have to go dig it up. Also have a good one for Hamiltonian Cycles, again in C++.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
|
I need just a simple console program to tell how many days and hours (minutes if possible) until this persons next birthday.
He was born on July 26th 1983 at 8:00 pm.
I'm really new at this and I wanted to give him a small exe (console program) for his birthday.
This is all I oould come up with so far:
using System;
class myApp
{
public static void Main()
{
DateTime CurrTime = DateTime.Now;
DateTime JimBDay = DateTime.Compare(CurrTime, "08/01/2008");
Console.WriteLine("Jim's Birthdate: ", JimBDay);
Console.WriteLine("Jim's Birthday is July 26th at 8:00 pm");
Console.WriteLine("{0:F}", CurrTime);
// Need some code to say there are this many days and this many hours until your next birthday (minutes if possible).
Console.Read(); // wait
}
}
|
|
|
|
|
you can use subtract Method to get days and hours and minutes and seconds difference between two date.
see this code:
DateTime CurrTime = DateTime.Now;
DateTime JimBDay = new DateTime(1983,7,26,20,0,0);
TimeSpan span= CurrTime.Subtract(JimBDay);
now span has days and hours and minutes and seconds difference between jim birthday and now.
Human knowledge belongs to the world
|
|
|
|
|
|
JimBDay does not show up on Console.WriteLine("Jim's Birthdate is... ", JimBDay); it just says "Jim's Birthday is..."
and
"span" shows as 9132.03:46:30.4287493 / How can I format this to say...
This many months...
This many days...
This many hours...
and
This many minuts until your next birthday?
using System;
class myApp
{
public static void Main()
{
DateTime CurrTime = DateTime.Now;
Console.WriteLine("Right now the date and time are: {0:F}", CurrTime);
DateTime JimBDay = new DateTime(1983, 7, 26, 20, 0, 0);
Console.WriteLine("Jim's Birthdate is... ", JimBDay);
TimeSpan span = CurrTime.Subtract(JimBDay);
Console.WriteLine(span);
Console.Read();
}
}
|
|
|
|
|
You'll have to pick apart the pieces from the span. There's Days, Minutes, and other properties to investigate.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
try these properties:
Console.WriteLine(span.Days.ToString());
Console.WriteLine(span.Hours.ToString());
Console.WriteLine(span.Minutes.ToString());
Console.WriteLine(span.Seconds.ToString());
Console.WriteLine(span.Milliseconds.ToString());
Human knowledge belongs to the world
|
|
|
|
|
First you need to find the next birthday. Create a DateTime value for the birthday this year (from the current year and the month and day from the birthday). If it has passed, add a year to the value.
Now you just subtract the current date from the value, and you get a TimeSpan that holds the time difference.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
i have a client.exe and c# program. . my exe can open only with my program. my exe cannot open alone.when i click button1 on my program , client.exe will open. But when i double click to client.exe , client.exe cant open.
how can i do this ?
|
|
|
|
|
Please clean up your question. It really makes no sense and is confusing
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi there.
I have Visual Studio 2008.
I want to use this class HttpUtility.HtmlDecode
I couldn't find it in System.Web
Where is it ?
|
|
|
|
|
You have to add a System.Web.dll class library to your project's references.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
|
I see how you can bind to a checkedlistbox for one item to be checked. but what if you have multiple items selected. How can you bind that?
here would be my code for one checked item
fooCheckedListBox.DataBindings.Add("SelectedValue", fooDataSource, "SomePropertyinMyBindingSource");
But I have multiple selected values.
|
|
|
|
|
I have windows form where I am adding control at run time but I couldn't remove them all. Some of them get removed but some stays there. I have spent already 4 hours nothing making sense why I couldn't simply remove controls from the form.
this.Controls(btn);---> this is how iam adding Controls
foreach (Control c in this.Controls)---> this is how am removing-->no achievement
{
this.Controls.Remove(c);
}
=========
but if copy all the controls to an arraylist and then it works. What a nosense is this is C#
ArrayList a = new ArrayList();
foreach (Control c in this.Controls )
{
a.Add(c);
}
foreach (Control cc in a)
{
this.Controls.Remove(cc);
}
modified on Saturday, July 26, 2008 4:01 PM
|
|
|
|
|
Every iteration of your foreach loop is incrementing an index,
but you're removing a control so all the other controls are shifted
up one in the collection. That means you'll miss every other control I
suppose
Maybe something like this:
for (int i = this.Controls.Count - 1; i >= 0; i--)
{
this.Controls.RemoveAt(i);
}
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote:
for (int i = this.Controls.Count - 1; i >= 0; i--)
{
this.Controls.RemoveAt(i);
}
...or...
foreach (Control c in Controls)
c.Dispose();
Controls.Clear();
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Much better
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
A honeyed cute version of yours I'd say.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|