|
I know one thing for sure, this problem is caused by streams. Please comment out all the close statements and test by uncommenting them one by one. ( I know its a layman solution). Still give it a shot.
One more thing - are you using streams elsewhere.
If you see the debug trace and call stack, you will realize that the exception occurs in the Paint event handler. Follow the details of the exception you get, you will get that answer. In the onpaint method, the GDI is accessing some null values (which you will have to find).
If nothing works, worst case scenario, the quickfix is override the OnPaint event handler and catch the exception but dont handle it.
protected override void OnPaint(PaintEventArgs e)
{
try
{
base.OnPaint (e);
}
catch
{
//Do Nothing
}
}
Live Life King Size
Alomgir Miah
|
|
|
|
|
Man, I think that you'll scold me a lot. The problem was I'm not breaking a "while" statement. Sorry, sorry, sorry. Thanks for help one more time and for the tips.
|
|
|
|
|
Never mind.
Live Life King Size
Alomgir Miah
|
|
|
|
|
I read a bitmap from file with the following code:
Bitmap tempBitmap = new Bitmap(bitmapFileName);
Later I want to save an updated version of the bitmap with this code:
if (System.IO.File.Exists(bitmapFileName)
System.IO.File.Delete(bitmapFileName);
tempBitmap.Save(bitmapFileName);
Unfortunately, when it gets to the delete, I get the following error:
"System.IO.IOException: The process cannot access the file "filename.bmp" because it is being used by another process."
The creation of the bitmap from file is holding on to the connection. I tried cloning and copying to the clipboard, but the only way I have been able to get this to work is by copying all my bitmaps to temp files and opening from the temp file. Then the file I delete and save to is a different file. I am hoping there is a better way.
|
|
|
|
|
The Bitmap object holds the file open for the lifetime of the object. The trick is to read the file yourself, create the bitmap from that stream, then close the stream.
FileStream fs = new FileStream(@"C:\bitmap.bmp");
Bitmap b = new Bitmap(fs);
fs.Close();
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
From MSDN,
You must keep the stream open for the lifetime of the Bitmap object.
Live Life King Size
Alomgir Miah
|
|
|
|
|
I tried using the Filestream and closing it after creating the bitmap. It is working. Why must the stream remain open?
|
|
|
|
|
If its working thats fine. But in some situations you might get Generic GDI+ error.
Check this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassctorTopic6.asp
So you should implement IDisposable interface and close the stream when Dispose Method is called.
I have written a BitmapHelper, hope this helps,
using System;
using System.IO;
using System.Drawing;
namespace Test.Controls
{
///
/// Summary description for BitmapHelper.
///
public class BitmapHelper: IDisposable
{
private Bitmap _bm;
private Icon _ic;
private MemoryStream _ms;
// Track whether Dispose has been called.
private bool disposed = false;
public BitmapHelper()
{
}
public void LoadBitmap(Stream sm)
{
Clear();
int len = (int) sm.Length;
byte[] buf = new byte[len];
sm.Read(buf,0,len);
_ms = new MemoryStream(buf);
_bm = (Bitmap) Image.FromStream(_ms);
}
public void LoadIcon(Stream sm)
{
Clear();
int len = (int) sm.Length;
byte[] buf = new byte[len];
sm.Read(buf,0,len);
_ms = new MemoryStream(buf);
_ic = new Icon(_ms);
}
public void LoadBitmap(string fileName)
{
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
LoadBitmap(fs);
fs.Close();
}
public void LoadIcon(string fileName)
{
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
LoadIcon(fs);
fs.Close();
}
public void LoadBitmap(object dbField)
{
Clear();
if (dbField != DBNull.Value)
{
byte[] buf = (byte[])dbField;
_ms = new MemoryStream(buf);
_bm = (Bitmap) Image.FromStream(_ms);
}
}
public void LoadIcon(object dbField)
{
Clear();
if (dbField != DBNull.Value)
{
byte[] buf = (byte[])dbField;
_ms = new MemoryStream(buf);
_ic = new Icon(_ms);
}
}
public void Clear()
{
if (_bm != null)
{
_bm.Dispose();
_bm = null;
}
if (_ic != null)
{
_ic.Dispose();
_ic = null;
}
if (_ms != null)
{
_ms.Close();
_ms = null;
}
}
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
this.Clear();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
}
disposed = true;
}
public Bitmap Bitmap
{
get
{
return _bm;
}
}
public Icon Icon
{
get
{
return _ic;
}
}
}
}
Live Life King Size
Alomgir Miah
|
|
|
|
|
Never have I had a problem with a Generic GDI error while using a Bitmap object. And I've created Bitmap objects from just about every imaginable source, be it a resource stream, external file, serialized network stream, XML data, SQL database, ... Come to think of it, I've never had a GDI+ error that wasn't caused by me doing something stupid, like trying to draw to the same object from two different threads at the same time.
If you have to keep the stream open, why does MSDN have an article that shows you how to get around this -> PRB: Image File Is Locked When You Set the PictureBox Image Property to a File[^]
The Bitmap class inherits from Image, so I fail to why you would have to keep the stream open for a Bitmap object and not for an Image object.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Closing the stream is working for me.
Thanks
|
|
|
|
|
Straight from MSDN
Remarks
You must keep the stream open for the lifetime of the Bitmap object.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassctorTopic6.asp
This happens sometimes when the OnPaint tries to access the BitMap and
gets a null.
Live Life King Size
Alomgir Miah
|
|
|
|
|
So it is...
But, like I said, never had a problem with it.
I fail to see why any Bitmap object (properly defined) would ever come back as null in the middle of OnPaint. I've never had it happen in any painting of any of my forms or controls.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Try adding a toolbar to your application. In each toolbar add a button.
Set an ImageList with BitMap streams. If you close the stream see what happens.
Live Life King Size
Alomgir Miah
|
|
|
|
|
How many times do I have to tell you, I've never had a problem with it! I know about that little quirk! That's why I NEVER keep my Toolbar images in files. I keep them in Resources! I hate having open files laying around.
I keep as many images as I can in resources. I've even developed an ImageResources resource .DLL that holds my common images, including Toolbar buttons, in various sizes, masks, and transparencies. It's completely static so I don't have to instantiate the thing either.
Instead of deploying a bunch of image files along with my apps, I only deploy a sinlge .DLL. Now, all I have to do to set a Toolbar button image is:
SaveButton.Image = ImageResources.Buttons.Retrieve("Save 16x16 Trans.bmp")
Obviously, Retrieve() returns a Bitmap object. And "Look Ma!," no open file handles!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Calm down man.
There is one more way, compile the images as embedded resources into a DLL, this way you have just one DLL. Still you have to open an stream.
FYI,
when you use resource files to embed images, a stream is opened internally. If you dont trust debug for yourself.
SO I am not fighting here, I am just trying to make my point.
Live Life King Size
Alomgir Miah
|
|
|
|
|
Alomgir Miah wrote:
when you use resource files to embed images, a stream is opened internally.
Ya! Why do you think I said "no open file handles"?
You just made it sound like there was only one way to create images, using FromFile, and that EVERY Bitmap had to have an open stream associated with it. That's just not true.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
lol u r tough.
lets wrap it up.
Live Life King Size
Alomgir Miah
|
|
|
|
|
I have to simulate a communication network over a city map. with features like route feasibilty study and viewing different components of the network .. please give me some guideline in this regard. how usefull C# would be in this regard.
haroon
|
|
|
|
|
C# would be great for this. The extent that you want to do simulation for would vary based on complexity but here is my first design thought off the top of my head:
1) You have a 'controller' form which displays your network topography -- nice and graphic etc. probably using the US map as background image and controls marking the nodes and something unique for router locations etc.
2) You will need a network stub abstract class which handles the basics such as number of hops to destination, avg hop delay, bandwidth between hops etc.
3) You would inherit the abstract class defining these variables for each specific location. Each abstract class would be represented by theUI node point.
4) Included in the abastract class would be Send(), Receive(), and RoundTrip() calls that do all of the math and lag computations to handle one-way and round-trip calls into that node. (ie. sending a 10-packet message from Home() to SanFrancisco() for example.)
If each node handle computations and also inherits MyCustomControl (which is the visual stuff on the screen) then when you mouse over the control it can pull up latest transmission results (lag time, roundtrip time, simulated ping, etc.)
Sounds like a really cool project.
There are 10 kinds of people in the world.
Those that read binary...
...and those who don't.
|
|
|
|
|
Hi!
I have to read a text file which can have text in languages like french, spanish etc.
and write the text to another text file.
When i use the StreamReader and StreamWriter with Encoding as Unicode i miss all the accented characters somehow.
Help will be appreciated
Best Wishes
|
|
|
|
|
I use an italian-localized machine, and I never found this kind of problem... try to read/write the file char-by-char, using BinaryReader.ReadChar() BinaryWriter.Write((char)yourChar)). I believe this way will work.
|
|
|
|
|
I Used "Encoding.Default" while reading the file and it worked.
I like your way also and i'll be trying it too..
Thank You
Best Wishes
|
|
|
|
|
Hello,
Is there a way of placing an event inside the DataGrid on leaving a particular cell, and an event on the Column (for example TextChanged).
Mine grid is: dataGridRevenues
mine table style: styleLotYields
Thank you
|
|
|
|
|
//
//Member Variables
//
int currentGridColumn = 0;
int currentGridRow = 0;
//
//Wire Up Events
//
this.dataGridDataEntrySheet.CurrentCellChanged += new EventHandler(this.OnGridCurrentCellChanged);
foreach( DataGridTextBoxColumn dataGridTextBoxColumn in this.dataGridTableStyle.GridColumnStyles )
{
dataGridTextBoxColumn.TextBox.TextChanged += new EventHandler(this.GridTextBoxTextChanged);
}
private void OnGridCurrentCellChanged(object sender, EventArgs e)
{
DataGridCell dataGridCell = this.dataGridDataEntrySheet.CurrentCell;
//Update Member Variables
this.currentGridRow = dataGridCell.RowNumber;
this.currentGridColumn = dataGridCell.ColumnNumber;
}
private void GridTextBoxTextChanged(object sender, EventArgs e)
{
if ( this.currentGridRow = 3 && this.currentGridColumn == 5 )
{
//Do some stuff
}
else if ( ... )
{
...
}
...
}
Live Life King Size
Alomgir Miah
|
|
|
|
|
Thank you very much
It helped a lot.
Another Question: If I have a column in a datagrid, with
MapingName = "Quantity"
How can I go through each cell for this particular column.
Thank you again
|
|
|
|