为什么inStream.Close()的位置;物?

时间:2014-03-22 16:35:26

标签: .net windows jpeg

为什么此代码有效

    Stream inStream;

    private void ResizePoster(string jpgFileName)
    {
        // Ideal poster size is 299×434 at 100% quality
        const int idealWidth = 299;
        const int idealHeight = 434;

        BitmapSource srcImage = DecodeJpg(jpgFileName);
        if (srcImage.PixelHeight > idealHeight || srcImage.PixelWidth > idealWidth)
        {
            float xScale = (float)idealWidth / srcImage.PixelWidth;
            float yScale = (float)idealHeight / srcImage.PixelHeight;

            float scale = Math.Min(xScale, yScale);
            TransformedBitmap scaleImage = new TransformedBitmap(srcImage,
                                        new ScaleTransform(scale,
                                                           scale,
                                                           0, 0));

            FileStream outStream = new FileStream(@"F:\Out.jpg", FileMode.Create);
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.QualityLevel = 100;
            encoder.Frames.Add(BitmapFrame.Create(scaleImage));
            encoder.Save(outStream);
            outStream.Close();
            inStream.Close();
        }
    }

    private BitmapSource DecodeJpg(string jpgFile)
    {
        // Open a Stream and decode a JPEG image
        inStream = new FileStream(jpgFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        JpegBitmapDecoder decoder = new JpegBitmapDecoder(inStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        BitmapSource bitmapSource = decoder.Frames[0];
        bitmapSource.Freeze();
        return bitmapSource;
    }

但不是这段代码吗?

    Stream inStream;

    private void ResizePoster(string jpgFileName)
    {
        // Ideal poster size is 299×434 at 100% quality
        const int idealWidth = 299;
        const int idealHeight = 434;

        BitmapSource srcImage = DecodeJpg(jpgFileName);
        if (srcImage.PixelHeight > idealHeight || srcImage.PixelWidth > idealWidth)
        {
            float xScale = (float)idealWidth / srcImage.PixelWidth;
            float yScale = (float)idealHeight / srcImage.PixelHeight;

            float scale = Math.Min(xScale, yScale);
            TransformedBitmap scaleImage = new TransformedBitmap(srcImage,
                                        new ScaleTransform(scale,
                                                           scale,
                                                           0, 0));

            FileStream outStream = new FileStream(@"F:\Out.jpg", FileMode.Create);
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.QualityLevel = 100;
            encoder.Frames.Add(BitmapFrame.Create(scaleImage));
            encoder.Save(outStream);
            outStream.Close();
            //inStream.Close();
        }
    }

    private BitmapSource DecodeJpg(string jpgFile)
    {
        // Open a Stream and decode a JPEG image
        inStream = new FileStream(jpgFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        JpegBitmapDecoder decoder = new JpegBitmapDecoder(inStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        BitmapSource bitmapSource = decoder.Frames[0];
        bitmapSource.Freeze();
        inStream.Close();
        return bitmapSource;
    }

唯一的区别是inStream.Close()的位置。我更喜欢在第一个中使用秒,以便我可以创建Stream inStream;方法变量而不是全局类变量。

我没有错误,但生成的jpg文件没有正确的内容。它是正确的尺寸,可以作为jpg打开,但图像是完整的垃圾。我正在使用.NET 4 Client Profile,如果这很重要的话。

以下是两张生成的图片,您可以自己查看:

Good out

Bad out

0 个答案:

没有答案