|
I have a gif file, 256 colors, created with Paint Shop Pro.
I do
// Load the bitmap
Bitmap myBitmap = new Bitmap ("toto.gif");
// Cut it
Bitmap frame = myBitmap.Clone(Rectangle);
// Save individual frames
frame.Save("Frame1.gif", Imaging.PixelFormat.Gif);
It works OK. Latter, I want to load the saved frame
// Load the frame again
Bitmap frame = new Bitmap("frame1.gif");
This works also. But, if I try to save it again
frame.Save("Frame2.gif");
I got an external exception in InteropServices, telling me there was a problem with GDI+.dll.
Anybody had the same problem?
|
|
|
|
|
I'm not sure exactly what you're trying to do (which may help to understand the problem and help you solve it), but take a look at the Image.SaveAdd method (which Bitmap would inherit, of course) to see how to save individual frames of an animated GIF, which I'm guessing is what you're trying to accomplish.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a storyboard, a single image that contains several frames. My program cut this storyboard in individual frames, of variable size.
I now want to save these individual frames in separate files.
This part works. The gif are indeed saved.
Later I need to make copies of some of these files, after I've done some processing on memory on them.
So I start by loading the individual frames doing
Bitmap bitmap = new Bitmap("Frame1.gif");
// Then some processing
// Then I save it again
bitmap.Save("Frame1.gif", PixelFormat.gif);
And there it crashes. I have tried to do the save immediatly after the load, in case my processing was messing something, but it still doesn't work.
Conclusion: I cannot save a bitmap immediately after I load it?
|
|
|
|
|
That is weird, and I frankly don't know the answer to that. But there is a better, more efficient way of copying files anyway: File.Copy .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Search on Google groups, I had the same problem a few years back. Sorry I cant recall the resolution though
top secret
|
|
|
|
|
Hello,
I have a question on Remoting that follows:
My first server configuration file has entry:
<service>
<wellknown mode="Singleton"
type="RemotingSamples.RemoteClass1,RemoteObject"
objectUri="ServiceEndPoint" />
</service>
My second server configuration file has entry:
<service>
<wellknown mode="Singleton"
type="RemotingSamples.RemoteClass2,RemoteObject"
objectUri="ServiceEndPoint" />
</service>
Now my client configuration file has entry:
<client>
<wellknown displayName="Server1"
type="RemotingSamples.RemoteClass1,RemoteObject"
url="tcp://machine1_ipaddress:8080/ServiceEndPoint" />
<wellknown displayName="Server2"
type="RemotingSamples.RemoteClass2,RemoteObject"
url="tcp://machine2_ipaddress:8086/ServiceEndPoint" />
</client>
I want user to select the server by giving server's machine ipaddress.
Once he gives me the machine_ipaddress I want to create object
of the class the selected server implements, that is when,
machine1_ipaddress is given I have to create instance of RemotingSamples.RemoteClass1
and
when machine2_ipaddress is given I have to create instance of RemotingSamples.RemoteClass2.
How do I do this?
Please give me more details (A simple implementaion if possible.)
|
|
|
|
|
First call RemotingConfiguration.Configure passing the configuration file with your remoting sections. Then you can use RemotingConfiguration.GetRegisteredWellKnownClientTypes to get a WellKnownClientTypeEntry[] array which you can enumerate:
private object GetInstance(string host)
{
string configfile = AppDomain.Current.SetupInformation.ConfigurationFile;
RemotingConfiguration.Configure(configfile);
WellKnownClientTypeEntry[] entries =
RemotingConfiguration.GetRegisteredWellKnownClientTypes();
foreach (WellKnownClientTypeEntry entry in entries)
{
Uri url = new Uri(entry.ObjectUrl);
if (string.Compare(host, url.Host, true) == 0)
{
object[] args = {new UrlAttribute(entry.ObjectUrl)};
return Activator.CreateInstance(entry.ObjectType, args);
}
}
return null;
} Something like that should work, but look through the documentation for the classes and members used to understand exactly what's going on.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I designed a form that got 2 textboxes(i named tbUserName & tbPassword) and 2 button (btOK & btCancel).
private void btOK_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Enter Key has been pressed");
}
tbPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((Control.ModifierKeys & Keys.Enter) == Keys.Enter)
btOK_Click(sender, e);
}
but nothing happen.
Please help me as possible as you can.
Thanks.
Mr Duc Linh Nguyen
|
|
|
|
|
duclinh wrote:
if ((Control.ModifierKeys & Keys.Enter) == Keys.Enter)
The Enter key is not a modifier key, so this will never be true. Here's what you're looking for:
if(e.KeyChar == (char)Keys.Enter)
However, you don't even need to do that. Just set your form's AcceptButton to be btOK and the button will be "clicked" when Enter is pressed in your form.
Charlie
if(!curlies){ return; }
|
|
|
|
|
What is the BEST / HIGHEST PERFORMANCE method of reading data from a SqlDataReader and returning a "default" value if the value from the reader is null. I have several different methods that all work but all involve calling a seperate void to test the values or catch the null exception. I just keep thinking there has to be a better way - i.e., one "slick" and simple line of code; but I can't seem to find it???
|
|
|
|
|
if (value == null) return defaultValue; Don't rely on exceptions for this purpose because they are expensive to catch and thus decrease performance. Also, value types (like numeric types, booleans, DateTime , etc.) can't be null, so you'll already get their default value (0, false, etc.) or DBNull depending on how things are set up).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Why won't this double buffer?
I'm drawing this graph that allows you to either move value sliders or input values and redraw the graph. I've looked at tons of double buffering examples and they all seem to be doing the same or similar type of code. But my graph still flickers. BAH!
[code]public void DrawGraph()
{
this.UpdateGraphVariables();
Point[] pointarray=new Point[5];
Pen newpen = new Pen(Color.Black);
this.m_bmImageOffScreen = new Bitmap(this.Width, this.Height);
this.m_gOffScreen = Graphics.FromImage(this.m_bmImageOffScreen);
this.m_gBuffer=Graphics.FromHwnd(this.Handle);
//computational stuff
//not interesting
I do all my drawing on the bitmap...
//Set point of origin to be 50,50 on the form
this.m_gOffScreen.TranslateTransform(50,130);
this.m_gOffScreen.DrawLine(newpen,1,1,1,150);
this.m_gOffScreen.DrawLine(newpen,1,150,150,150);
//Draw X and y axis
this.m_gOffScreen.DrawLine(newpen,-3,30,3,30);
this.m_gOffScreen.DrawLine(newpen,-3,60,3,60);
this.m_gOffScreen.DrawLine(newpen,-3,90,3,90);
...
//Join the points
pointarray[0]=new Point(30,Convert.ToInt32(a));
pointarray[1]=new Point(60,Convert.ToInt32(b));
pointarray[2]=new Point(90,Convert.ToInt32(c));
pointarray[3]=new Point(120,Convert.ToInt32(d));
pointarray[4]=new Point(150,Convert.ToInt32(f));
this.m_gOffScreen.DrawLines(new Pen(Color.Yellow),pointarray);
Then I attempt to double buffer!
if (this.m_bmOldBitmap != null)
{
this.m_bmOldBitmap.Dispose();
}
this.m_bmOldBitmap = this.m_bmImageOffScreen;
this.m_gBuffer.Clear(this.BackColor);
this.m_gBuffer.DrawImage(this.m_bmOldBitmap,30,50);
}[/code]
It still flickers!
|
|
|
|
|
In InitializeComponent put SetStyle(ControlStyles.DoubleBuffer);
|
|
|
|
|
I've got that!
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
|
|
|
|
|
You also need ControlStyles.AllPaintingInWmPaint , which is documented in the ControlStyles enumeration documentation.
Also, that enum is attributed with the FlagsAttribute , which means you can save your code some time and just use:
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true); This gets compiled as a constant value so SetStyle is only called once.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Do I do one or the other or do I set the styles AND do the manual double buffering witht the bitmaps and the two graphics objects?
|
|
|
|
|
Have you enabled double buffering in your form?
Do this in your initialisation (I put it in my Form constructor after the IntializeComonent() call.
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
|
|
|
|
|
Nope, that didn't help.
|
|
|
|
|
How do you call your DrawGraph( ) function?
|
|
|
|
|
The DrawGraph() function is called from OnPaint()
Invalidate is called every time the draw graph button is clicked or a track bar is moved changing one of the values in the graph.
protected override void OnPaint(PaintEventArgs e)
{
if(this.m_bmImageOffScreen==null)
this.m_bmImageOffScreen=new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
this.m_gOffScreen =Graphics.FromImage(this.m_bmImageOffScreen);
//this.m_gBuffer.Clear(this.BackColor);
if(this.m_bgraphDrawn)
this.DrawGraph();
else
{
this.textBox1.Text = "0";
this.textBox2.Text = "0";
this.textBox3.Text = "0";
this.textBox4.Text = "0";
this.textBox5.Text = "0";
this.m_bgraphDrawn = true;
this.DrawGraph();
}
this.m_gOffScreen.Dispose();
e.Graphics.DrawImageUnscaled(this.m_bmImageOffScreen,30,50);
}
private void trackBar5_Scroll(object sender, System.EventArgs e)
{
// Display the trackbar value in the text box.
textBox5.Text = "" + trackBar5.Value;
value5 += trackBar1.Value;
//this.redrawChart();
//this.DrawGraph();
Invalidate();
}
|
|
|
|
|
what is a better way to implement a server with multiple clients ,using an Event driven handling or to init new
threads to handle each Event?
|
|
|
|
|
I guess it depends on what you mean by "server", which is a rather generic term. If you're referring to a socket listener, then waiting for a connection and passing the new request off to a thread is a good idea. You can also use the event-driven method and use BeginInvoke (it may not show up in IntelliSense, but it's there) which invokes the handler asynchronously.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
According to MSDN doc, when I derive a class that implements IDisposable, I need to override protected virtual void Dispose(bool disposing). And in this function, if disposing is true, I need to call Dispose() on all managed objects that I own (in addition to releasing unmanaged resources).
Now, when I create a new Form object, the wizard gives me the overridden Dispose(bool) method with the only statement being:
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
However, if I add a control, say a button named button1, it doesn't add the statement button1.Dispose() into the above method. Is this a mistake? I mean, do I need to manually add this statement, or is it already called implicitly somewhere?
|
|
|
|
|
First, the Dispose(bool) method doesn't NOT need to be overridden and is not declared in IDisposable . IDisposable declares one method: void Dispose() . Dispose(bool) is a virtual method inheritted from Control . You can implement IDisposable on anything. You don't need to override Dispose(bool) unless you contain components that need to be disposed, like AxHost controls (ActiveX controls in a managed environment) and stuff like that.
Managed objects are managed by the garbage collector. They will be garbage collected when necessary. You typically implement IDisposable (or override Dispose(bool) in your controls) when you need to clean-up unmanaged resources like window handles, file handles, and the like. These things are unmanaged, i.e. they are not managed by the garbage collector (GC). Objects that don't need to free unmanaged resources can implement and use IDisposable to free managed resources, but should use GC.SurpressFinalize(this) in the Dispose() implementation to prevent the GC from wasting time cleaning it up (since you already did).
Other reasons for implementing IDisposable and for callers to call Dispose is to release resources immediately since the GC doesn't run continuously. This makes sure that resources (even managed resources) are released when you want them to be. For things like a Button , it typically doesn't matter. The GC will get around to it. Dispose it only if it's absolutely necessary (otherwise you degrade the performance of your application since the GC is taking time away from your program's execution).
See Programming for Garbage Collection[^] in the .NET Framework SDK for more information and examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Actually I wrote the email after reading the Proramming for Garbage Collection article. I guess the reason why I need to Dispose() managed objects like Button() is if holds unmanaged resources, such as maybe icon, brush, etc. But now I realise that I probably don't need to call Dispose() directly on these controls, because when I call base.Dispose(bool) in the Dispose(bool) override that I have in my form, it will presumably call Dispose() for every control that it has (in the Controls collection). Other components such as Timer will be disposed when I call components.Dispose(). This is a guess, which I will verify later on.
|
|
|
|
|