Parameter is not valid for new Bitmap in .net

Recently from nowhere we bumped into the error Parameter is not valid for new Bitmap in .net

You get this error when you try to create a new bitmap from a image object. After comparing all the parameters for normal image and error image we identified that the error image was having the pixelformat as 8207 whereas the normal image had pixelformat as Format24bppRgb.

parameter is not valid in new bitmap having PixelFormat 8207

PixelFormat 8207

After reading about this we identified this happens with Indexed Color Images. Color information is not directly carried by image pixel data.

The possible solution for parameter is not valid for new bitmap

we could find was to create a bitmap from image filestream and then clone that bitmap with pixelformat.

So from

Image fileImg = Image.FromStream(fileStream);

We changed it to

Bitmap bm = new Bitmap(fileStream);
Bitmap cloneImg = null;
if (bm.PixelFormat.ToString() == "8207")
{
RectangleF cloneRect = new RectangleF(0, 0, bm.Width, bm.Height);
System.Drawing.Imaging.PixelFormat format = bm.PixelFormat.ToString() == "8207" ? PixelFormat.Format24bppRgb : bm.PixelFormat;
cloneImg = bm.Clone(cloneRect, format);
}
Image fileImg = bm.PixelFormat.ToString() == "8207" ? cloneImg : bm;

After cloning the bitmap object from above code we set the pixelformat for new image to Format24bppRgb which the new Bitmap understand and allows to create an object. We then use this  fileImg object to create a new bitmap object without any parameter is not valid for new bitmap error.

Parameter is not valid for new bitmap before cloning the bitmap

Pixel format before cloning the bitmap

 

Parameter is not valid for new bitmap solved after cloning the bitmap

Pixel format for new image after cloning

If you have any better solution do let me know.

The operation has timed out with HttpWebRequest

Recently we were using HttpWebRequest. Things were working fine until we bumped into, “The Operation has timed out”.

ERROR

 The operation has timed out

It was running perfectly fine but all of a sudden it stopped working. Later we identified that the request URL was HTTPS. We just changed HTTPS to HTTP and the code worked again. But changing HTTPS to HTTP is not the solution. It is a quick fix.

The site with HTTPS was working in the browser. We got all the responses from the browser. However, We get this error with HttpWebRequest. HttpWebRequest worked with Http URL but not with HTTPS.

Later we started to look into the problem. We made sure our firewall was configured correctly. We contacted the third party guys they said they are not getting the request with HTTPS. It was clear there is some problem. We figured out that HttpWebRequest got stuck at a stage where it’s supposed to send back a client key exchange which it didn’t.

Solution

Then we forced HttpWebRequest to use SSL3 instead of TLS. Although TLS automatically turns into SSL3 but it din’t happen. So the fix to the problem was

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

 Just place the above line before calling GetResponse() and that was it.

To read more about ServicePointManager – http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.securityprotocol(v=vs.110).aspx