Hassaan Masood
Hassaan Masood

Reputation: 145

Bitmap Parameter Invalid

I am facing an error when trying to create a bitmap directly from a string that contains file name and path.

My Code is described below:

if (!string.IsNullOrEmpty(Request.QueryString["imagename"]))
    {
        string Image = Request.QueryString["imagename"];

        Bitmap originalBMP = new Bitmap(Server.MapPath(@"UserImages/" + Image));

        // Calculate the new image dimensions
        int origWidth = originalBMP.Width;
        int origHeight = originalBMP.Height;
        int sngRatio = origWidth / origHeight;
        int newWidth = 50;
        int newHeight = newWidth / sngRatio;

        // Create a new bitmap which will hold the previous resized bitmap
        Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);

        // Create a graphic based on the new bitmap
        Graphics oGraphics = Graphics.FromImage(newBMP);
        // Set the properties for the new graphic file
        oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

        // Draw the new graphic based on the resized bitmap
        oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
        // Save the new graphic file to the server

        Response.ContentType = "image/PNG";
        newBMP.Save(Response.OutputStream, ImageFormat.Png);

        originalBMP.Dispose();
        newBMP.Dispose();
        oGraphics.Dispose();
    }

But the following code is producing an error that Parameter is Invalid:

Bitmap originalBMP = new Bitmap(Server.MapPath(@"UserImages/" + Image));

Upvotes: 0

Views: 1642

Answers (2)

Joe Johnston
Joe Johnston

Reputation: 2936

For winforms What ChrisBint posted is also valid and proper. In addition be sure to set the resource/image as available for your executable.

eg Copy to output : Always Copy

enter image description here

Upvotes: 0

Kinexus
Kinexus

Reputation: 12904

It is probably down to the fact that the file itself does not exist.

You can check for the existence of that file using

var fileName =Server.MapPath(@"UserImages/" + Image);
if (File.Exists(fileName)
{
   //Existing code here
}

Alternatively, you could flip this code and alert the user

if (!File.Exists(fileName)
{
   //Throws exception/Alert user here
}

Upvotes: 3

Related Questions