1
Imagename = objUser.UserID + filename;
Imagepath = "D:\\Shop\\ShopMonkey\\Images" + Imagename;
FileUpload.SaveAs(Path.Combine(@"D:\ShopMonkey_Web_21-6-12\ShopMonkey\Images", Imagename));

objUser.UploadImagePath = Imagepath;
objUser.UploadImagename = Imagename;

System.Drawing.Image img1 = System.Drawing.Image.FromFile(Imagepath);

System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero);
 ThumbNailPath = "D:\\ShopMonkey_Web_21-6-12\\ShopMonkey\\ThumbNails" + Imagename;
bmp1.Save(Path.Combine(@"D:\ShopMonkey_Web_21-6-12\ShopMonkey\ThumbNails", Imagename));
objUser.UploadThumbnailPath = ThumbNailPath;

他の機能で画像とサムネイルを削除するには?(最初にそれを閉じる必要がありますか?)

4

1 に答える 1

1

同じユーザーによる画像の asp.net アップロード時に、このユーザーのファイルが既に存在する場合、ディスク上のファイルを削除しようとしていると思います。

このメソッドは、画像とサムネイルが存在する場合、両方を削除します。

イメージの作成アクティビティとイメージのクリーンアップ アクティビティを分けて、意図を明確にし、コードを保守しやすくします。

    // replace with an entry loaded from a config file    
    const string ImageRoot = @"D:\ShopMonkey_Web_21-6-12\ShopMonkey"; 
    // replace this is your user instance
    object user = new object(); 
    string Imagename = objUser.UserID + filename;
    string uploadImagePath = Path.Combine(ImageRoot, "Images", Imagename);
    string thumbnailPath = Path.Combine(ImageRoot, "ThumbNails", Imagename);
    objUser.UploadImagePath = uploadImagePath;
    objUser.UploadImagename = Imagename;
    objUser.UploadThumbnailPath = thumbnailPath;
    // delete both if they exist
    if (File.Exists(uploadImagePath))
        File.Delete(uploadImagePath);
    if (File.Exists(thumbnailPath))
          File.Delete(thumbnailPath);
    // replace this with your uploaded file details
    object fileInfo = new object(); 
    using (System.Drawing.Image img1 = System.Drawing.Image.FromFile(uploadImagePath)) {
          img1.Save(uploadImagePath);
          using (System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero)) {
            bmp1.Save(thumbnailPath);
          }
          FileUpload.SaveAs(uploadImagePath);
    }
于 2012-06-23T12:54:49.837 に答える