asp.net c# convert filestream to image

帅比萌擦擦* 提交于 2019-12-10 20:47:07

问题


i have a filestream which im trying to convert into a image. i have the following code

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

image is a map holding information on the image. please can some one direct me on what I should do next.


回答1:


Image.FromStream will take a stream and return an image. Incidentally, if you use Bitmap.FromStream or Image.FromStream, or indeed any of these methods, they all return a Bitmap, so they can all be upcast to Bitmap from Image if you want them to be.




回答2:


you can do the following:

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


来源:https://stackoverflow.com/questions/8848725/asp-net-c-sharp-convert-filestream-to-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!