Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to convert FormatConvertedBitmap to Stream in C#
    primarykey
    data
    text
    <p>I am new to C#.</p> <p>I want to convert <code>FormatConvertedBitmap</code> to <code>Stream</code> 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.</p> <p>can some one help me in either :</p> <ol> <li>Converting FormatConvertedBitmap to an Stream Or</li> <li>Writing FormatConvertedBitmap to a file and then reading it as Stream.</li> </ol> <p>public Stream Image</p> <pre><code> { 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; } </code></pre> <p>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.</p> #####<strong><em>EDIT</em></strong> <pre><code>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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload