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. Saving Bitmap in HashTable

Saving Bitmap in HashTable

Scheduled Pinned Locked Moved C#
csharpgraphicshelp
9 Posts 4 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.
  • V Offline
    V Offline
    vivekphlp
    wrote on last edited by
    #1

    Iam new to C#. . . Here is my my requirement . . I want to save some images into a Hashtable & retrieve it later. . . How can it be done.. . Plz help

    Proud To Be an Indian

    F 1 Reply Last reply
    0
    • V vivekphlp

      Iam new to C#. . . Here is my my requirement . . I want to save some images into a Hashtable & retrieve it later. . . How can it be done.. . Plz help

      Proud To Be an Indian

      F Offline
      F Offline
      free_soul424
      wrote on last edited by
      #2

      Use the following code, i think it will work... public partial class Form1 : Form { Hashtable imgHash = new Hashtable(); public Form1() { InitializeComponent(); /* create new bitmap*/ Bitmap bmp = new Bitmap(100, 100); Rectangle rect = new Rectangle(12, 12, 50, 50); Graphics g = Graphics.FromImage(bmp); g.DrawRectangle(new Pen(Color.Red), rect); /* put the bitmap in hashtable*/ imgHash.Add("1", bmp); } private void Form1_Paint(object sender, PaintEventArgs e) { if(imgHash != null) this.CreateGraphics().DrawImage((Image)imgHash["1"] ,new Point (0,0)); } }

      M 1 Reply Last reply
      0
      • F free_soul424

        Use the following code, i think it will work... public partial class Form1 : Form { Hashtable imgHash = new Hashtable(); public Form1() { InitializeComponent(); /* create new bitmap*/ Bitmap bmp = new Bitmap(100, 100); Rectangle rect = new Rectangle(12, 12, 50, 50); Graphics g = Graphics.FromImage(bmp); g.DrawRectangle(new Pen(Color.Red), rect); /* put the bitmap in hashtable*/ imgHash.Add("1", bmp); } private void Form1_Paint(object sender, PaintEventArgs e) { if(imgHash != null) this.CreateGraphics().DrawImage((Image)imgHash["1"] ,new Point (0,0)); } }

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello,

        faheem424 wrote:

        this.CreateGraphics().

        Why would you want todo that? "e.Graphics" is the wright solution.´

        faheem424 wrote:

        imgHash["1"]

        Wouldn't do that without a "Hashtable.Contains" check before.

        All the best, Martin

        F 1 Reply Last reply
        0
        • M Martin 0

          Hello,

          faheem424 wrote:

          this.CreateGraphics().

          Why would you want todo that? "e.Graphics" is the wright solution.´

          faheem424 wrote:

          imgHash["1"]

          Wouldn't do that without a "Hashtable.Contains" check before.

          All the best, Martin

          F Offline
          F Offline
          free_soul424
          wrote on last edited by
          #4

          yup you may change the condition with the following code if (imgHash.Contains("1")) e.Graphics.DrawImage((Image)imgHash["1"], new Point(0, 0));

          T 1 Reply Last reply
          0
          • F free_soul424

            yup you may change the condition with the following code if (imgHash.Contains("1")) e.Graphics.DrawImage((Image)imgHash["1"], new Point(0, 0));

            T Offline
            T Offline
            thenewbee
            wrote on last edited by
            #5

            Thanks a lot . . can i save that image back to a Bitmap eg: bmp = imgHash["1"]; hope it wil work.. .

            M 1 Reply Last reply
            0
            • T thenewbee

              Thanks a lot . . can i save that image back to a Bitmap eg: bmp = imgHash["1"]; hope it wil work.. .

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, As the Hashtable holds the Bitmap instance as an object, you will get an compiletime error telling you that an implicit conversion from object to Bitmap is not possible! Therefor you would have to do the casting yourselfe. If you are 100% sure that the Hashtable only holds Bitmaps: if(ht.Contains("1")) { Bitmap bmp2 = (Bitmap)ht["1"]; } or: Bitmap bmp2 = obmp as Bitmap; if(bmp2!=null) { }

              All the best, Martin

              T 1 Reply Last reply
              0
              • M Martin 0

                Hello, As the Hashtable holds the Bitmap instance as an object, you will get an compiletime error telling you that an implicit conversion from object to Bitmap is not possible! Therefor you would have to do the casting yourselfe. If you are 100% sure that the Hashtable only holds Bitmaps: if(ht.Contains("1")) { Bitmap bmp2 = (Bitmap)ht["1"]; } or: Bitmap bmp2 = obmp as Bitmap; if(bmp2!=null) { }

                All the best, Martin

                T Offline
                T Offline
                thenewbee
                wrote on last edited by
                #7

                Thanks Again . :) Is there any diff in creating obj using Bitmap bmp2 = new Bitmap(); & Bitmap bmp2 = obmp as Bitmap; What is this obmp???

                M 1 Reply Last reply
                0
                • T thenewbee

                  Thanks Again . :) Is there any diff in creating obj using Bitmap bmp2 = new Bitmap(); & Bitmap bmp2 = obmp as Bitmap; What is this obmp???

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  Hello,

                  thenewbee wrote:

                  What is this obmp???

                  Sorry, forgot that: object obmp = ht["1"]; //just gets the Bitmap as an object out of the Hashtable.

                  thenewbee wrote:

                  Is there any diff in creating obj using Bitmap bmp2 = new Bitmap(); & Bitmap bmp2 = obmp as Bitmap;

                  "new Bitmap" instanciates a Bitmap object, which was not there before. "as Bitmap" tries to convert an existing object to a Bitmap (which was instanciated before). "as" will never throw an exception, therefor I often use it instead of a cast like "(Bitmap)object".

                  All the best, Martin

                  T 1 Reply Last reply
                  0
                  • M Martin 0

                    Hello,

                    thenewbee wrote:

                    What is this obmp???

                    Sorry, forgot that: object obmp = ht["1"]; //just gets the Bitmap as an object out of the Hashtable.

                    thenewbee wrote:

                    Is there any diff in creating obj using Bitmap bmp2 = new Bitmap(); & Bitmap bmp2 = obmp as Bitmap;

                    "new Bitmap" instanciates a Bitmap object, which was not there before. "as Bitmap" tries to convert an existing object to a Bitmap (which was instanciated before). "as" will never throw an exception, therefor I often use it instead of a cast like "(Bitmap)object".

                    All the best, Martin

                    T Offline
                    T Offline
                    thenewbee
                    wrote on last edited by
                    #9

                    Thanks Martin . . .. :)

                    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