System.Drawing.Image to stream C#
Try the following:
public static Stream ToStream(this Image image, ImageFormat format) {
var stream = new System.IO.MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}
Then you can use the following:
var stream = myImage.ToStream(ImageFormat.Gif);
Replace GIF with whatever format is appropriate for your scenario.
Convert a System.Drawing.Image to a stream
You can "Save" the image into a stream.
If you need a stream that can be read elsewhere, just create a MemoryStream
:
var ms = new MemoryStream();
image.Save(ms, ImageFormat.Png);
// If you're going to read from the stream, you may need to reset the position to the start
ms.Position = 0;
How to save image as stream?
You have to initialize your stream before using it.
using (MemoryStream myStream = new MemoryStream()) {
screenshot.Save(myStream, System.Drawing.Imaging.ImageFormat.Gif);
}
System.Drawing.Image data is different between saving to a file and a stream
They are not different but when you dump or copy or do something else with memory stream you always have to reset it to its initial position.
fileImage.Save(bmpStream, System.Drawing.Imaging.ImageFormat.Bmp);
bmpStream.Position = 0
... now you can dump or save to file from bmpStream
If you do not reset the position, you might read nothing back from the MemoryStream. In the case of Image.Save()
, it is even more tricky because the Save method puts the MemoryStream position at the beginning of the image data (after the header) assuming this is what you want.
Load Image from Stream
Right, so I managed to fix it:
var bitMap = new BitmapImage();
stream.seek(0); // LINE ADDED
bitMap.SetSourceAsync(stream);
image.Source = bitMap;
I turned out that the error that was being produced was : "The component cannot be found.", so I managed to fix it by using this trick.
Unable to create System.Drawing.Image object from WebResponse Stream
Resetting the stream position does not work on the Stream base class. If you can read the response into a MemoryStream, then you will be able to manipulate the image as you please. Something like this could work:
using (WebResponse response = await request.GetResponseAsync())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream memStream = new MemoryStream())
{
responseStream.CopyTo(memStream);
// some code that calls a 3rd party image resizer, passing in the original stream
ResizeImage(memStream, out resizedOutputStream);
memStream.Seek(0, SeekOrigin.Begin);
// manipulation of the resizedOutputStream
// now i want to create an image from the ORIGINAL stream
using (Image image = Image.FromStream(memStream))
{
}
}
}
}
Copy MemoryStream and recreate System.Drawing.Image fails with Parameter is not valid
Before reading from the stream
you need to rewind its position to zero. You are doing that for the copy right now, but also need to do that for the original.
Also, you don't need to copy to a new stream at all.
I usually resolve such problems by stepping through the program and looking at runtime state to see whether it matches my expectation or not.
Related Topics
How to Check If a List Is Ordered
C# Drag Drop Does Not Work on Windows 7
How to Provide Custom Cast Support for My Class
How to Show File Copy Progress Using Fileinfo.Copyto() in .Net
Pattern for Calling Wcf Service Using Async/Await
Convert Base Class to Derived Class
How to Get the Width and Height of a Multi-Dimensional Array
How to Initialize a List<T> to a Given Size (As Opposed to Capacity)
Mvvm Light 5.0: How to Use the Navigation Service
Dependency Injection Using Azure Webjobs Sdk
JSON String to CSV and CSV to JSON Conversion in C#
Could Not Load File or Assembly 'Microsoft.Reportviewer.Common, Version=11.0.0.0
How to Convert Datetime To/From Specific String Format (Both Ways, E.G. Given Format Is "Yyyymmdd")
Updating an Item Property Within Ienumerable But the Property Doesn't Stay Set
How to Get the Last Five Characters of a String Using Substring() in C#