在c#中处理图像对象的最佳实践是什么
本文关键字:最佳 是什么 对象 处理 图像 | 更新日期: 2023-09-27 18:25:34
我有以下两段代码:第一段没有显式处理Image对象,而第二段正确地处理了它。请建议在生产代码中使用哪一个。
private bool SavePatientChartImage(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME, int rotationAngle)
{
bool success = false;
System.Drawing.Image newImage;
try
{
using (MemoryStream stream = new MemoryStream(ImageBytes))
{
newImage = System.Drawing.Image.FromStream(stream);
switch (rotationAngle)
{
case 90:
newImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 180:
newImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 270:
newImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
newImage = newImage;
break;
}
newImage.Save(Path.Combine(ImageFilePath, IMAGE_NAME));
success = true;
}
}
catch (Exception ex)
{
success = false;
}
return success;
}
和
private bool SavePatientChartImage(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME, int rotationAngle)
{
bool success = false;
System.Drawing.Image newImage;
try
{
using (MemoryStream stream = new MemoryStream(ImageBytes))
{
using(newImage = System.Drawing.Image.FromStream(stream))
{
switch (rotationAngle)
{
case 90:
newImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 180:
newImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 270:
newImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
newImage = newImage;
break;
}
newImage.Save(Path.Combine(ImageFilePath, IMAGE_NAME));
success = true;
}
}
}
catch (Exception ex)
{
success = false;
}
return success;
}
要虔诚地遵循哪一个。请建议
您应该始终将可丢弃实例丢弃在某个地方。所以拿后者来说。
你可以让它更可读:
using (MemoryStream stream = new MemoryStream(ImageBytes))
using(var newImage = System.Drawing.Image.FromStream(stream))
{
// ...
注意:在using语句之外声明变量是没有意义的。你不应该在外面用它。