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の値を設定する条件ブロックを使用しない場合、またはアップロードした画像のみをそのまま保存する場合は、この例外は発生しません。
ありがとうございました
[C#Image.Clone Out of Memory Exception]の可能な複製(https://stackoverflow.com/questions/199468/c-sharp-image-clone-out-of-memory-exception) –
**ビットマップをフリーズする**ことができます。私の質問の答えを確認してください:https://stackoverflow.com/questions/26300379/large-1-bit-per-pixel-bitmap-cause-outofmemoryexception –
正確にビットマップをフリーズしますか?あなたは例を共有することはできますか? – PeeBee