Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中处理图像对象的最佳实践是什么?_C# - Fatal编程技术网

在c#中处理图像对象的最佳实践是什么?

在c#中处理图像对象的最佳实践是什么?,c#,C#,我有以下两段代码第一段没有显式地处理Image对象,而第二段正确地处理它。请建议在生产代码中使用哪一个 private bool SavePatientChartImage(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME, int rotationAngle) { bool success = false; System.Drawing.Image newImage; t

我有以下两段代码第一段没有显式地处理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;
    }


哪一个是宗教信仰。请建议

您应该始终在某个地方处置一次性实例。就拿后者来说吧

您可以让它更具可读性:

       using (MemoryStream stream = new MemoryStream(ImageBytes))
       using(var newImage = System.Drawing.Image.FromStream(stream))
       {
         // ...

注意:在using语句之外声明变量是没有意义的。你不应该在外面使用它。

在编程中,很少有人会虔诚地遵循任何建议。编程语言中的每个特性都是为某个用例添加的,因此每当您遇到该用例时(这可能很少见,例如C goto),您都应该使用该特性。可能会有帮助。就我个人而言,我只会使用第二个,因为它没有那么难,完成后应该正确地处理资源,但我不是专家。
       using (MemoryStream stream = new MemoryStream(ImageBytes))
       using(var newImage = System.Drawing.Image.FromStream(stream))
       {
         // ...