Saving Bitmap in HashTable
-
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
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)); } }
-
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)); } }
-
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
yup you may change the condition with the following code if (imgHash.Contains("1")) e.Graphics.DrawImage((Image)imgHash["1"], new Point(0, 0));
-
yup you may change the condition with the following code if (imgHash.Contains("1")) e.Graphics.DrawImage((Image)imgHash["1"], new Point(0, 0));
-
Thanks a lot . . can i save that image back to a Bitmap eg: bmp = imgHash["1"]; hope it wil work.. .
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
-
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
-
Thanks Again . :) Is there any diff in creating obj using Bitmap bmp2 = new Bitmap(); & Bitmap bmp2 = obmp as Bitmap; What is this obmp???
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
-
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