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

本文关键字:转换 图像 文件 net asp | 更新日期: 2023-09-27 18:20:07

我有一个文件流,我正试图将其转换为图像。我有以下代码

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

图像是保存关于图像的信息的地图。请有人指导我下一步该做什么好吗。

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

Image.FromStream将获取流并返回图像。顺便说一句,如果您使用Bitmap.FromStreamImage.FromStream,或者实际上使用这些方法中的任何一个,它们都会返回一个Bitmap,因此如果您希望它们是,它们都可以从Image向上转换到Bitmap

您可以执行以下操作:

        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);