0

ファイル名とパスを含む文字列からビットマップを直接作成しようとすると、エラーが発生します。

私のコードは以下のとおりです。

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();
    }

ただし、次のコードは、パラメーターが無効であるというエラーを生成しています。

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

2 に答える 2

3

ファイル自体が存在しないことが原因である可能性があります。

を使用してそのファイルの存在を確認できます

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

または、このコードを反転してユーザーに警告することもできます

if (!File.Exists(fileName)
{
   //Throws exception/Alert user here
}
于 2012-08-02T08:31:24.200 に答える
0

winforms の場合 ChrisBint が投稿したものも有効で適切です。さらに、リソース/イメージを実行可能ファイルで使用できるように設定してください。

例 出力にコピー: 常にコピー

ここに画像の説明を入力

于 2013-12-20T18:20:24.447 に答える