2

I am new to C#.

I want to convert FormatConvertedBitmap to Stream as said earlier. I couldn't find any method for that. I thought to save or write the FormatConvertedBitmap to a file so that reading it as a Stream, but even couldn't find a way to write it to a file.

can some one help me in either :

  1. Converting FormatConvertedBitmap to an Stream Or
  2. Writing FormatConvertedBitmap to a file and then reading it as Stream.

public Stream Image

    {
        get
    {//some condition
                if (_image != null)
                {
                    _image.Seek(0, SeekOrigin.Begin);

                    BitmapImage bmp = new BitmapImage();
                    MemoryStream stream = (MemoryStream)_image;
                    bmp.BeginInit();
                    bmp.StreamSource = stream;
                    bmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                    bmp.EndInit();
                    var grayBitmapSource = new FormatConvertedBitmap();
                    grayBitmapSource.BeginInit();
                    grayBitmapSource.Source = bmp;
                    grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
                    grayBitmapSource.EndInit();
                    Stream st=new MemoryStream();
                    //File.WriteAllBytes(Path.GetTempPath(),grayBitmapSource);
                    return grayBitmapSource;
                }

In the above code I am getting the Image as Stream from the server then on some condition I am converting it to grayScale image . but now we sould return an Stream and finaly we have FormatConvertedBitmap.

#####EDIT
public Stream Image
        {
            get
            {
                //some condition

                            if (_image != null)
                            {
                                _image.Seek(0, SeekOrigin.Begin);
                                BitmapImage bmp = new BitmapImage();
                                MemoryStream stream = (MemoryStream)_image;
                                bmp.BeginInit();
                                bmp.StreamSource = stream;
                                bmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                                bmp.EndInit();
                                var grayBitmapSource = new FormatConvertedBitmap();
                                grayBitmapSource.BeginInit();
                                grayBitmapSource.Source = bmp;
                                grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
                                grayBitmapSource.EndInit();
                                int bytePerPixel = grayBitmapSource.Format.BitsPerPixel / 8;
                                int width = grayBitmapSource.PixelWidth;
                                int height = grayBitmapSource.PixelHeight;
                                int stride = width * bytePerPixel;
                                byte[] resultLine = new byte[height * stride];
                                grayBitmapSource.CopyPixels(resultLine, stride, 0);
                                MemoryStream ms = new MemoryStream(resultLine);
                                return ms;
Sohail Faruqui
  • 442
  • 11
  • 27

1 Answers1

1

You can use the method CopyPixels to write the contents of your FormatConvertedBitmap to a byte array.

You can then use that byte array to either Initialize a MemoryStream or store them to a file with BmpBitmapEncoder or other BitmapEncoder.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • 1
    thanx , i would appreciate if you write the code for CopyPixels as well. i tried the code (Edited in question) which is not producing the gray effect, though the code for making the image gray works fine when i tried on other project. – Sohail Faruqui Jan 22 '13 at 07:53
  • I did not know how to use copyPixels so i found the code some where, so i am not sure about the Strid value an all – Sohail Faruqui Jan 22 '13 at 08:01
  • @SohailAnwerFaruqui The stride is the number of bytes per row of the image (so, it looks good as you have it). About the gray effect, I don't know... my guess is that it may be when you interpretate the image because you have to remember that it has Gray32Float pixel format. – Theraot Jan 22 '13 at 08:13
  • @SohailAnwerFaruqui if you have problems with stride hopefully this answer will help you: http://stackoverflow.com/questions/3881857/bitmapsource-copypixels-whats-a-good-value-for-stride Also, here you can find some relevant code: http://wrb.home.xs4all.nl/Articles_2010/Article_WPFImageTIFF_01.htm (in that code they add 7 to the bitsperpixel so they get the stride aligned to multiples of 8 by the rounding). – Theraot Jan 22 '13 at 08:27