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 :
- Converting FormatConvertedBitmap to an Stream Or
- 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.
#####EDITpublic 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;