3

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.

c11ada
  • 4,242
  • 14
  • 45
  • 62
  • Why are you duplicating the `image` variable name? I doubt what you have will compile. – Oded Jan 13 '12 at 10:00
  • 3
    What do you mean 'convert to an image' - do you mean turn it into a `Bitmap` instance or serve it up to a client in response to an image request? – Andras Zoltan Jan 13 '12 at 10:01
  • the image is on a server and I get it using a FileStream, the idea is to display the image on a web page. – c11ada Jan 13 '12 at 10:08

2 Answers2

5

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.

AlexB
  • 6,970
  • 12
  • 51
  • 71
cgraus
  • 734
  • 2
  • 9
  • 25
1

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);
Salvatore Di Fazio
  • 679
  • 2
  • 6
  • 24