How Do I convert BitmapSource to MemoryStream. Though I tried some code:
private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
Stream bmp;
using (bmp = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(writeBmp));
enc.Save(bmp);
}
return bmp;
}
It doesn't give any error but after putting debugging point it is showing some exceptions which are listed below.
Capacity: 'printStream.Capacity' threw an exception of type
'System.ObjectDisposedException' Length: 'printStream.Length' threw
an exception of type 'System.ObjectDisposedException' Position:
'printStream.Position' threw an exception of type
'System.ObjectDisposedException'
using (bmp = new MemoryStream()) causes bmp object is destroyed on end using block. And You return bmp variable which is destroyed.
Remove using:
private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
Stream bmp = new MemoryStream();
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(writeBmp));
enc.Save(bmp);
return bmp;
}
The problem here is that you are creating bmp inside an using, that's why it has been disposed before you return it (it is disposed once you leave the using) and that explains the exceptions you receive.
private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
Stream bmp= new MemoryStream();
using (enc = new BmpBitmapEncoder())
{
enc.Frames.Add(BitmapFrame.Create(writeBmp));
enc.Save(bmp);
}
return bmp;
}
Related
i wanna save a picture that loaded in a picturebox to stream .when i save in png format it work properly but when i want save it in other formats i get
A Generic error occured in GDI + exception
its my code:
Image Img = pictureBox1.Image;
byte[] inputImage = new byte[Img.Width * Img.Height];
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Read(inputImage, 0, Img.Width * Img.Height);
if (System.Drawing.Imaging.ImageFormat.Jpeg.Equals(Img.RawFormat))
{
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (System.Drawing.Imaging.ImageFormat.Gif.Equals(Img.RawFormat))
{
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
}
else if (System.Drawing.Imaging.ImageFormat.Png.Equals(Img.RawFormat))
{
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
}
else if (System.Drawing.Imaging.ImageFormat.Bmp.Equals(Img.RawFormat))
{
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
}
Try this method:
public Stream ImageToStream(Image image, System.Drawing.Imaging.ImageFormat format)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, format);
return ms;
}
and use it:
using(Stream stream = ImageToStream(pictureBox1.Image,
System.Drawing.Imaging.ImageFormat.Gif))
{
...
}
I need to create an Image from a Byte Array but I don't know how to do this. I tried to do it like this:
using (var ms = new MemoryStream(byteArrayIn))
{
return Image.FromStream(ms);
}
But there was always the message that the parameter ms is not valid.
The exact Exception message is:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
With this I am reading the Array from the database
byte[] bytes = ObjectToByteArray(reader["profilepicture"]);
private byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
Can anybody please help me with this problem?
Try this:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Source
I am getting an error while instantiating ExifReader in Windows Phone 8 C#. Please find the code snippet below. Kindly do the needful
Error : "ExifLib requires a seekable stream"
byte[] imageBytes = (byte[])PhoneApplicationService.Current.State["ViewImage"];
MemoryStream ms = new MemoryStream(imageBytes,0,imageBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
try
{
ExifReader xif = new ExifReader(toStream(bitmapImage)); // Getting Error here
double gpsLat, gpsLng;
xif.GetTagValue<double>(ExifTags.GPSLatitude, out gpsLat);
xif.GetTagValue<double>(ExifTags.GPSLongitude, out gpsLng);
map.Center = new System.Device.Location.GeoCoordinate(gpsLat, gpsLng);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Stream toStream(BitmapImage img)
{
WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img);
using (MemoryStream stream = new MemoryStream())
{
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
stream.Position = 0;
return stream;
}
}
Your MemoryStream is returning false when CanSeek is called. This is because you've wrapped your MemoryStream in a using statement, which means that you're returning a disposed object.
Your toStream method should actually look like this:
Stream ToStream(BitmapImage img)
{
MemoryStream stream = new MemoryStream();
using (WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img))
{
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
}
stream.Position = 0;
return stream;
}
I have tryed this but have exception - Operation is not valide due to current state of the object
private BitmapFrame backconvertor(byte[] incomingBuffer)
{
BitmapImage bmpImage = new BitmapImage();
MemoryStream mystream = new MemoryStream(incomingBuffer);
bmpImage.StreamSource = mystream;
BitmapFrame bf = BitmapFrame.Create(bmpImage);
return bf;
}
Error rising when I am trying to
return backconvertor(buff);
in other function (buff - is ready!)
Documentation indicates that in order to initialize the image, you need to do it between BeginInit and EndInit. That is:
bmpImage.BeginInit();
bmpImage.StreamSource = mystream;
bmpImage.EndInit();
Or, you can pass the stream to the constructor:
bmpImage = new BitmapImage(mystream);
See http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.begininit.aspx for an example and more discussion of BeginInit.
This is what I have in a WPF Converter to handle byte to BitmapFrame and it works perfectly:
var imgBytes = value as byte[];
if (imgBytes == null)
return null;
using (var stream = new MemoryStream(imgBytes))
{
return BitmapFrame.Create(stream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
Also its thread safe as I have used it in Task.Run before also.
Using C#, I'm trying to load a JPEG file from disk and convert it to a byte array. So far, I have this code:
static void Main(string[] args)
{
System.Windows.Media.Imaging.BitmapFrame bitmapFrame;
using (var fs = new System.IO.FileStream(#"C:\Lenna.jpg", FileMode.Open))
{
bitmapFrame = BitmapFrame.Create(fs);
}
System.Windows.Media.Imaging.BitmapEncoder encoder =
new System.Windows.Media.Imaging.JpegBitmapEncoder();
encoder.Frames.Add(bitmapFrame);
byte[] myBytes;
using (var memoryStream = new System.IO.MemoryStream())
{
encoder.Save(memoryStream); // Line ARGH
// mission accomplished if myBytes is populated
myBytes = memoryStream.ToArray();
}
}
However, executing line ARGH gives me the message:
COMException was unhandled. The handle is invalid. (Exception from
HRESULT: 0x80070006 (E_HANDLE))
I don't think there is anything special about the file Lenna.jpg - I downloaded it from http://computervision.wikia.com/wiki/File:Lenna.jpg. Can you tell what is wrong with the above code?
Check the examples from this article: http://www.codeproject.com/KB/recipes/ImageConverter.aspx
Also it's better to use classes from System.Drawing
Image img = Image.FromFile(#"C:\Lenna.jpg");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
arr = ms.ToArray();
}
Other suggestion:
byte[] image = System.IO.File.ReadAllBytes ( Server.MapPath ( "noimage.png" ) );
Should be working not only with images.
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
The reason this error happens is because the BitmapFrame.Create() method you are using defaults to an OnDemand load. The BitmapFrame doesn't try to read the stream it's associated with until the call to encoder.Save, by which point the stream is already disposed.
You could either wrap the entire function in the using {} block, or use an alternative BitmapFrame.Create(), such as:
BitmapFrame.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);