How do I load a BMP file that has been added to my solution as an embedded resource

If you add a BMP file to your solution using the File|Add Existing Item… Menu Item, then change the Build Action property of this BMP file to Embedded Resource, you can then access this resource with code similar to: // WindowsApplication6 corresponds to Default Namespace in your project settings. // subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don’t have to specify anything here. string bmpName = ‘WindowsApplication6.subfolders.sync.bmp’; System.IO.Stream strm = null; try { strm = this.GetType().Assembly.GetManifestResourceStream(bmpName); pictureBox1.Image = new Bitmap(strm); // Do the same for Icons // Icon icon1 = new Icon(strm); } catch(Exception e) { MessageBox.Show(e.Message); } finally { if(strm != null) strm.Close(); }

How do I create a new bitmap with a new size based on an existing bitmap?

You can simply specify the new size in the Bitmap constructor as follows: [C#] Bitmap bmp = new Bitmap(‘exisiting.bmp’); // Create a new bitmap half the size: Bitmap bmp2 = new Bitmap(bmp, bmp.Width*0.5, bmp.Height*0.5); this.BackgroundImage = bmp2; [VB.Net] Dim bmp As New Bitmap( ‘exisiting.bmp’) ’ Create a new bitmap half the size: Dim bmp2 As New Bitmap( bmp, bmp.Width * 0.5, bmp.Height * 0.5) Me.BackgroundImage = bmp2 If you have to specify a particular Interpolation mode while resizing use the following code: [C#] Bitmap bmp = new Bitmap(‘exisiting.bmp’); // Create a new bitmap half the size: Bitmap bmp2 = new Bitmap( bmp.Width*0.5, bmp.Height*0.5, Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmp2); // Set interpolation mode g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // Draw image using specified interpolation mode. g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height); this.BackgroundImage = bmp2 [VB.Net] Dim bmp As New Bitmap( ‘existing.bmp’) Dim bmp2 As New Bitmap( bmp.Width * 0.5, bmp.Height * 0.5, Imaging.PixelFormat.Format24bppRgb) Dim g As Graphics = Graphics.FromImage(bmp2) ’ Set interpolation mode g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic ’ Draw image using specified interpolation mode. g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height) Me.BackgroundImage = bmp2

Windows Forms Programming in C#

Windows Forms Programming in C# by Chris Sells ISBN: 0321116208 An excellent book for learning Windows Forms using C# for both Beginners and experienced Programmers. The presentation is nice and crisp the chapter size is usually around 50 pages, presenting all the necessary details and at the same time maintaining the reader’s interest in the topic being presented.

While deserializing how do I check whether a name is available in the deserialized info?

This is usually an issue when a newer version introduces newer names, then the older version will not serialize property due to the absence of certain names. For example, this code will fail, sometimes: protected MyClassConstructor(SerializationInfo info, StreamingContext context) { … // This might fail if MyProp was added in a newer version and you are serializing an older version. this.MyProp = info.GetBoolean(‘MyProp’); } To avoid such conflicts, you could insert version nos. into the serialized info. and during deserialization check for a name only when a particular version is being deserialized. Or you could instead parse through the available info in the SerializationInfo list as follows: [C#] protected MyClassConstructor(SerializationInfo info, StreamingContext context) { foreach(SerializationEntry entry in info) { switch(entry.Name) { case ‘MyProp’: // This will make sure that older versions without the MyProp name will also deserialize without any problems this.MyProp = (bool)entry.Value; break; … } } } [VB.Net] Protected MyClassConstructor(ByVal info As SerializationInfo, ByVal context As StreamingContext) As Protected Dim entry As SerializationEntry For Each entry In info Select Case entry.Name Case ‘MyProp’ ’ This will make sure that older versions without the MyProp name will also deserialize without any problems Me.MyProp = (Boolean)entry.Value Exit For End Select Next End Function