5

画像ファイルを取得し、サイズを変更してから、別の名前で同じフォルダーに保存します ( filename+"-resize" )が、このエラーが発生します

A generic error occurred in GDI+

メソッドのサイズを変更するための私のコードは次のとおりです。

private  string resizeImageAndSave(string imagePath)
{
    System.Drawing.Image fullSizeImg
         = System.Drawing.Image.FromFile(Server.MapPath(imagePath));
    var thumbnailImg = new Bitmap(565, 290);
    var thumbGraph = Graphics.FromImage(thumbnailImg);
    thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
    thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
    thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    var imageRectangle = new Rectangle(0, 0, 565, 290);
    thumbGraph.DrawImage(fullSizeImg, imageRectangle);
    string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath),     Path.GetFileNameWithoutExtension(imagePath) + "-resize");
    thumbnailImg.Save(targetPath, ImageFormat.Jpeg); //(A generic error occurred in GDI+) Error occur here !
    thumbnailImg.Dispose();
    return targetPath;
}

私はそれを修正する方法を知りたいですか?

4

10 に答える 10

7

他の人が言ったように、それは許可の問題であるか、ディレクトリが存在しない可能性があります。ただし、イメージを保存する前に複製を試みることもできます。上記に問題がない場合は、これで問題が解決する可能性があります。

private static string resizeImageAndSave(string imagePath)
{
    System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imagePath));
    var thumbnailImg = new Bitmap(565, 290);
    var thumbGraph = Graphics.FromImage(thumbnailImg);
    thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
    thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
    thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    var imageRectangle = new Rectangle(0, 0, 565, 290);
    thumbGraph.DrawImage(fullSizeImg, imageRectangle);
    fullSizeImg.Dispose(); //Dispose of the original image
    string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "-resize");
    Bitmap temp = thumbnailImg.Clone() as Bitmap; //Cloning
    thumbnailImg.Dispose();
    temp.Save(targetPath, ImageFormat.Jpeg); 
    temp.Dispose();
    return targetPath;
}
于 2013-06-25T16:13:36.320 に答える
4

このコード行の後...

string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath),     Path.GetFileNameWithoutExtension(imagePath) + "-resize");

これを試してください...拡張子付きの画像名を追加してください

if (!Directory.Exists(targetPath))
   Directory.CreateDirectory(targetPath);
//now do the rest and place image name and extension...
thumbnailImg.Save(targetPath + @"\img.jpg", ImageFormat.Jpeg); 
thumbnailImg.Dispose();
return targetPath;
于 2013-06-27T02:47:20.377 に答える
1

作成中のパスを確認してください。保存する前に以下を使用して、ディレクトリ パスが存在することを確認してください:-

Directory.CreateDirectory(Path.GetDirectoryName(fileName));

また、ソース ファイルに保持されているロックを発行することを検討してください。これにより、コピーを作成して既存の参照を破棄できます。

var thumbnailImgNew = new Bitmap(thumbnailImg);
thumbnailImg.Dispose(); 
thumbnailImg=null;
//Save it to target path
thumbnailImgNew.Save(targetPath, ImageFormat.Jpeg); 
于 2013-06-24T06:41:53.973 に答える
1

許可の問題のようです。

ASP.NET が書き込みできるように、宛先フォルダーのアクセス許可を変更します。

ファイルを保存するユーザーは、あなたではなく ASP.NET ユーザーであることに注意してください。

于 2013-06-21T08:17:01.320 に答える
1

このコードを試してください:

    private string resizeImageAndSave(byte[] imageBytes, string fileName) {
        var mem = new MemoryStream(imageBytes);
        System.Drawing.Image fullSizeImg = System.Drawing.Image.FromStream(mem);

        var thumbnailImg = new Bitmap(565, 290);
        var thumbGraph = Graphics.FromImage(thumbnailImg);
        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
        thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

        var imageRectangle = new Rectangle(0, 0, 565, 290);
        thumbGraph.DrawImage(fullSizeImg, imageRectangle);

        string targetPath = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileNameWithoutExtension(fileName) + "-resize.jpg");

        thumbnailImg.Save(targetPath, ImageFormat.Jpeg); //(A generic error occurred in GDI+) Error occur here !
        thumbnailImg.Dispose();

        return targetPath;
    }

    protected void SaveButton_Click(object sender, EventArgs e) {
        var inStream = Request.Files[0].InputStream;
        var buff = new byte[inStream.Length];

        inStream.Read(buff, 0, buff.Length);

        resizeImageAndSave(buff, Request.Files[0].FileName);
    }
于 2013-06-27T09:27:38.910 に答える
1

あなたのコードから大きな問題を見つけることができないので、ファイルを書き込む許可を確認する必要があると思います。

于 2013-06-21T08:18:13.170 に答える