Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generic GDI+ error

Generic GDI+ error

Scheduled Pinned Locked Moved C#
winformsgraphicshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alomgir Miah
    wrote on last edited by
    #1

    I am using image streams to set the images of my toolbar. If I close the streams, I get generic GRI+ error. On the other hand if I dont close them, the error is resolved. Any ideas?

    M 1 Reply Last reply
    0
    • A Alomgir Miah

      I am using image streams to set the images of my toolbar. If I close the streams, I get generic GRI+ error. On the other hand if I dont close them, the error is resolved. Any ideas?

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      Straight from MSDN Image.FromStream docs: Remarks You must keep the stream open for the lifetime of the Image object. I created a Bitmap helper class that copied the bytes to MemoryStream and managed the Bitmap from there. Here are some of the methods of the class:

      public void Load(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 Load(string fileName)
      {
          FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
          Load(fs);
          fs.Close();
      }
      
      public void Load(object dbField)
      {
          Clear();
          if (dbField != DBNull.Value)
          {
              byte[] buf = (byte[])dbField;
              _ms = new MemoryStream(buf);
              _bm = (Bitmap) Image.FromStream(_ms);
          }
      }
      
      public void Clear()
      {
          if (_bm != null)
          {
              _bm.Dispose();
              _bm = null;
          }
          if (_ms != null)
          {
              _ms.Close();
              _ms = null;
          }
      }
      
      public Bitmap Bitmap
      {
          get {return _bm;}
      }
      
      A 1 Reply Last reply
      0
      • M Michael Potter

        Straight from MSDN Image.FromStream docs: Remarks You must keep the stream open for the lifetime of the Image object. I created a Bitmap helper class that copied the bytes to MemoryStream and managed the Bitmap from there. Here are some of the methods of the class:

        public void Load(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 Load(string fileName)
        {
            FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
            Load(fs);
            fs.Close();
        }
        
        public void Load(object dbField)
        {
            Clear();
            if (dbField != DBNull.Value)
            {
                byte[] buf = (byte[])dbField;
                _ms = new MemoryStream(buf);
                _bm = (Bitmap) Image.FromStream(_ms);
            }
        }
        
        public void Clear()
        {
            if (_bm != null)
            {
                _bm.Dispose();
                _bm = null;
            }
            if (_ms != null)
            {
                _ms.Close();
                _ms = null;
            }
        }
        
        public Bitmap Bitmap
        {
            get {return _bm;}
        }
        
        A Offline
        A Offline
        Alomgir Miah
        wrote on last edited by
        #3

        Thanks a lot.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups