asp.net c#将文件流转换为图像

时间:2012-01-13 09:58:39

标签: c# asp.net filestream

我有一个文件流,我试图转换成图像。我有以下代码

FileStream imageFile = image["file"] as FileStream;

图像是保存图像信息的地图。请有人指导下我应该做什么。

2 个答案:

答案 0 :(得分:5)

Image.FromStream将获取一个流并返回一张图片。顺便提一下,如果您使用Bitmap.FromStreamImage.FromStream,或者实际上使用这些方法中的任何一种,它们都会返回Bitmap,因此它们都可以从{{1}转发到Bitmap如果你想要它们。

答案 1 :(得分:1)

您可以执行以下操作:

        int width = 128;
        int height = width;
        int stride = width / 8;
        byte[] pixels = new byte[height * stride];

        // Define the image palette
        BitmapPalette myPalette = BitmapPalettes.Halftone256;

        // Creates a new empty image with the pre-defined palette
        BitmapSource image = BitmapSource.Create(
            width,
            height,
            96,
            96,
            PixelFormats.Indexed1,
            myPalette,
            pixels,
            stride);

        FileStream stream = new FileStream("new.jpg", FileMode.Create);
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.FlipHorizontal = true;
        encoder.FlipVertical = false;
        encoder.QualityLevel = 30;
        encoder.Rotation = Rotation.Rotate90;
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(stream);