2017-06-12 15 views
0

ASPファイルアップロードコントロールを使用してイメージをアップロードしようとしていますが、中央から四角形に切り抜こうとしています。これは、C#でビットマップを切り抜いたときのSystem.OutOfMemoryException

public void CropImage(string imagePath) 
    { 
     //string imagePath = @"C:\Users\Admin\Desktop\test.jpg"; 
     Bitmap croppedImage; 
     int x, y; 
     // Here we capture the resource - image file. 
     using (var originalImage = new Bitmap(imagePath)) 
     { 
     if (originalImage.Width > originalImage.Height) { 
      x = 0; 
      y = (originalImage.Width/2) - (originalImage.Height/2); 
     } 
     else if (originalImage.Width < originalImage.Height) 
     { 
      y = 0; 
      x = (originalImage.Height/2) - (originalImage.Width/2); 
     } 
     else { 
      y=x=0; 
     } 
      int sqrWidth = (originalImage.Width >= originalImage.Height) ? originalImage.Height : originalImage.Width; 
      Rectangle crop = new Rectangle(x, y, sqrWidth, sqrWidth); 

      // Here we capture another resource. 
      croppedImage = originalImage.Clone(crop, originalImage.PixelFormat); 

     } // Here we release the original resource - bitmap in memory and file on disk. 

     // At this point the file on disk already free - you can record to the same path. 
     croppedImage.Save(imagePath, ImageFormat.Jpeg); 

     // It is desirable release this resource too. 
     croppedImage.Dispose(); 
    } 
    //Updates an agent in the database. 
    protected void BtnUpdate_Click(object sender, EventArgs e) 
    { 
     AgentController agentController = new AgentController(); 
     AgentContactController agentContactController = new AgentContactController(); 
     CityController cityController = new CityController(); 
     DistrictController districtController = new DistrictController(); 
     ProvinceController provinceController = new ProvinceController(); 
     CountryController countryController = new CountryController(); 
     InsurancePortalContext context = new InsurancePortalContext(); 



     string folderPath = Server.MapPath("~/AdvisersImages/"); 

     //Check whether Directory (Folder) exists. 
     if (!Directory.Exists(folderPath)) 
     { 
      //If Directory (Folder) does not exists. Create it. 
      Directory.CreateDirectory(folderPath); 
     } 

     //Save the File to the Directory (Folder). 
     string FilePath = folderPath + Path.GetFileName(ImageUploadUpdate.PostedFile.FileName); 

     if (!File.Exists(FilePath)) 
     { 
      ImageUploadUpdate.SaveAs(FilePath); 
      CropImage(FilePath); 
     } 

私のコードですが、私は

croppedImage = originalImage.Clone(作物、originalImage.PixelFormat)、で、メモリ例外のうちを取得しています。

CropImage関数でx、yの値を設定する条件ブロックを使用しない場合、またはアップロードした画像のみをそのまま保存する場合は、この例外は発生しません。

ありがとうございました

+0

[C#Image.Clone Out of Memory Exception]の可能な複製(https://stackoverflow.com/questions/199468/c-sharp-image-clone-out-of-memory-exception) –

+0

**ビットマップをフリーズする**ことができます。私の質問の答えを確認してください:https://stackoverflow.com/questions/26300379/large-1-bit-per-pixel-bitmap-cause-outofmemoryexception –

+0

正確にビットマップをフリーズしますか?あなたは例を共有することはできますか? – PeeBee

答えて

2

コメントに記載されているとおりです。 .Clone()クロップ矩形がイメージの境界から外れている場合は、OutOfMemoryExceptionをスローします。あなたは右のそれをここまで読むことができます:
幅:5pxの
高さ:10pxの

これはあなたのコードである
https://msdn.microsoft.com/de-de/library/ms141944(v=vs.110).aspx

には、次のサイズが入力された画像を使用してコードを考えてみましょう。

if(originalImage.Width > originalImage.Height) 
{ 
    x = 0; 
    y = (originalImage.Width/2) - (originalImage.Height/2); 
} 
else if(originalImage.Width < originalImage.Height) 
{ 
    y = 0; 
    x = (originalImage.Height/2) - (originalImage.Width/2); 
} 
else 
{ 
    y = x = 0; 
} 

int sqrWidth = (originalImage.Width >= originalImage.Height) ? originalImage.Height : originalImage.Width; 
Rectangle crop = new Rectangle(x, y, sqrWidth, sqrWidth); 

5x10イメージはelse ifに該当します。
yはに設定され、xは((10/2)= 5) - (5/2)= 2)= に設定されます。
sqrWidthはに設定されます。

次矩形クローンしようとする次へ:
のx = 3、yは= 10、H = 10

wは、0を=とあなたの矩形がイメージの境界の外で出来上がり。クロッピングロジックを修正する必要があります。

関連する問題