Wednesday, June 25, 2008

Find ComboBox disappeared in VS 2005

I had this problem when I installed VS 2005.  The fix is in this message board

http://forums.asp.net/p/1109112/1709908.aspx

The solution:

1. Right click the toolbar

2. Select Customize...

3. Drag and drop the "Edit | Go To Find Combo" from "Commands" tab to the Standard toolbar.



Monday, June 16, 2008

Bitmap Clone vs new Bitmap vs ???

I was loading a Bitmap Image from a FileStream and calling the Image.Clone function.  When I tried to save the Image to another file I got the following error "A generic error occurred in GDI+", below is the exact code I was using:

In  Class1.Function1 I had the following:

public Bitmap Functon1(string tempFileName)
{
                Bitmap bm;
                System.IO.FileStream fs;
                fs = new FileStream(tempFileName, FileMode.Open, FileAccess.Read);
                try
                {
                    bm = (Bitmap)System.Drawing.Bitmap.FromStream(fs);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    fs.Close(); // I want to make sure the stream is closed
                }

                return (Bitmap)bm.Clone();
}
I placed the returned Bitmap into an ArrayList, called ListType.  Later on in the program I did the following and got the GDI+ error:
                pic.Image = (Bitmap)ListType[i];
                Bitmap tempImage = (Bitmap)pic.Image.Clone();
                tempImage.Save(@"c:\temp\file.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

One solution was to replace, and I no longer got my error.
                Bitmap tempImage = (Bitmap)pic.Image.Clone();
With
                Bitmap tempImage = new Bitmap(pic.Image);

That seemed to work.  However, another developer that was using my program was using an Image Library that did a deep clone, and whenever he ran his clone method on the image in my ArrayList he got the GDI+ error.  Therefore, I felt that I had to change my Function1.  I found a solution on the following website:
http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/970a7951-19f3-4ca3-8061-683b96c11a88/

The solution in my Function was:
        static public Bitmap Function1 (string tempFileName)
        {
            try
            {              
                using (FileStream fs = new FileStream(@tempFileName, FileMode.Open))
                {
                    int len = (int)fs.Length;
                    byte[] buf = new byte[len];
                    fs.Read(buf, 0, len);
                    using (MemoryStream ms = new MemoryStream(buf))
                    {
                        return new Bitmap(ms);
                    }
                }
         }

This solved the problem when the other developer called his deep clone method.  The explanation from the author of this block of code was:
GDI+ holds on to the stream handle and puts a lock on the file. Cloning is handled by GDI+, it probably copies the file handle too. You'll find that saving back the image to the same file will generate an exception too. The workaround is to load the image into memory first so GDI+ can't lock anything. For example:

Anyways, thats about it.

-- Ted



Thursday, June 12, 2008

"The page cannot be displayed" error for chm files

I had some chm files that I've always been able to open and read.  However, recently I moved them to other folders when all of a sudden I was not able to view the individual pages in these files.  I got a page cannot be displayed error.  It turns out that the reason is that I placed these chm files in a folder that had a "#".  These were C# books, so I created a folder called "C#"and placed the chm files in this folder.  This is what caused my problem.  Once I renamed the folder to "C Sharp" I was able to open the chm files.

Get VSS Password & User ID auto-entered by VSS

To get your VSS password & user ID auto-entered by VSS you need to define environment variables that contain this info.

Here is how to do it:

1.      Right-click My Computer and then click Properties.
2.      In the System Properties dialog box, click Advanced, and then click Environment Variables.
3.      Under User variables click New.
4.      In the New User Variable dialog box, in the Variable name box, type "SSUSER".
5.      In the Variable Value box, type your VSS username, and then click Ok.
6.      Repeat these steps to add "SSPWD" as an environment variable whose value is your VSS password.

Convert a Bitmap to a Raw Image in C#

I have a C dll that takes a Bitmap and writes it out to a Raw Image file, but was looking for a solution in C# and found this code on the following site: http://www.developerfusion.co.uk/forums/p/27321/160103/#160103

/// <summary>
/// Convert a bitmap to a byte array
/// </summary>
/// <param name="bitmap">image to convert</param>
/// <returns>image as bytes</returns>
private byte[] ConvertBitmap(Bitmap bitmap)
{
    //Code excerpted from Microsoft Robotics Studio v1.5
    BitmapData raw = null;  //used to get attributes of the image
    byte[] rawImage = null; //the image as a byte[]

    try
    {
        //Freeze the image in memory
        raw = bitmap.LockBits(
            new Rectangle(0, 0, (int)bitmap.Width, (int)bitmap.Height),
            ImageLockMode.ReadOnly,
            PixelFormat.Format24bppRgb
        );

        int size = raw.Height * raw.Stride;
        rawImage = new byte[size];

        //Copy the image into the byte[]
        System.Runtime.InteropServices.Marshal.Copy(raw.Scan0, rawImage, 0, size);
    }
    finally
    {
        if (raw != null)
        {
            //Unfreeze the memory for the image
            bitmap.UnlockBits(raw);
        }
    }
    return rawImage;
}