Click here to Skip to main content
16,006,475 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic GDI+ error Pin
Alomgir Miah25-Jul-05 3:55
Alomgir Miah25-Jul-05 3:55 
GeneralRe: Generic GDI+ error Pin
Alex Cutovoi25-Jul-05 4:15
Alex Cutovoi25-Jul-05 4:15 
GeneralRe: Generic GDI+ error Pin
Alomgir Miah25-Jul-05 4:34
Alomgir Miah25-Jul-05 4:34 
GeneralReleasing bitmap file after open Pin
Rick Beideman25-Jul-05 2:55
Rick Beideman25-Jul-05 2:55 
GeneralRe: Releasing bitmap file after open Pin
Dave Kreskowiak25-Jul-05 4:23
mveDave Kreskowiak25-Jul-05 4:23 
GeneralRe: Releasing bitmap file after open Pin
Alomgir Miah25-Jul-05 4:31
Alomgir Miah25-Jul-05 4:31 
GeneralRe: Releasing bitmap file after open Pin
Rick Beideman25-Jul-05 4:46
Rick Beideman25-Jul-05 4:46 
GeneralRe: Releasing bitmap file after open Pin
Alomgir Miah25-Jul-05 4:59
Alomgir Miah25-Jul-05 4:59 
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
GeneralRe: Releasing bitmap file after open Pin
Dave Kreskowiak25-Jul-05 5:32
mveDave Kreskowiak25-Jul-05 5:32 
GeneralRe: Releasing bitmap file after open Pin
Rick Beideman25-Jul-05 5:38
Rick Beideman25-Jul-05 5:38 
GeneralRe: Releasing bitmap file after open Pin
Alomgir Miah25-Jul-05 5:57
Alomgir Miah25-Jul-05 5:57 
GeneralRe: Releasing bitmap file after open Pin
Dave Kreskowiak25-Jul-05 6:32
mveDave Kreskowiak25-Jul-05 6:32 
GeneralRe: Releasing bitmap file after open Pin
Alomgir Miah25-Jul-05 6:58
Alomgir Miah25-Jul-05 6:58 
GeneralRe: Releasing bitmap file after open Pin
Dave Kreskowiak25-Jul-05 9:31
mveDave Kreskowiak25-Jul-05 9:31 
GeneralRe: Releasing bitmap file after open Pin
Alomgir Miah25-Jul-05 12:00
Alomgir Miah25-Jul-05 12:00 
GeneralRe: Releasing bitmap file after open Pin
Dave Kreskowiak26-Jul-05 1:10
mveDave Kreskowiak26-Jul-05 1:10 
GeneralRe: Releasing bitmap file after open Pin
Alomgir Miah26-Jul-05 3:58
Alomgir Miah26-Jul-05 3:58 
GeneralNetwork Simulation Pin
harooniq25-Jul-05 2:44
harooniq25-Jul-05 2:44 
GeneralRe: Network Simulation Pin
LongRange.Shooter25-Jul-05 9:54
LongRange.Shooter25-Jul-05 9:54 
GeneralWriting Accented characters to text files Pin
SilverStag25-Jul-05 2:42
SilverStag25-Jul-05 2:42 
GeneralRe: Writing Accented characters to text files Pin
Dario Solera25-Jul-05 5:04
Dario Solera25-Jul-05 5:04 
GeneralRe: Writing Accented characters to text files Pin
SilverStag25-Jul-05 19:56
SilverStag25-Jul-05 19:56 
GeneralDataGrid events Pin
zaboboa25-Jul-05 2:21
zaboboa25-Jul-05 2:21 
GeneralRe: DataGrid events Pin
Alomgir Miah25-Jul-05 3:30
Alomgir Miah25-Jul-05 3:30 
GeneralRe: DataGrid events Pin
zaboboa25-Jul-05 3:53
zaboboa25-Jul-05 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.