6

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.

3
  • Why are you duplicating the image variable name? I doubt what you have will compile. Commented Jan 13, 2012 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? Commented Jan 13, 2012 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. Commented Jan 13, 2012 at 10:08

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.