Why can't I create an image from image array - c#

I'm getting an error very similar to A generic error occurred in GDI+, JPEG Image to MemoryStream
However, I don't believe I'm shutting down the stream and as such, the answers don't apply
Please consider
var img = (Tests.Properties.Resources.image).ToByteArray(); //img is a png;
using (var ms = new MemoryStream(img))
{
var pic = Image.FromStream(ms);
pic.Save(this._absolutePath, this._format); //kaboomn
}
The issue is the final line of code goes kaboom!
"A generic error occurred in GDI+."
This is the extension method
public static byte[] ToByteArray(this Bitmap image)
{
using (var ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
return ms.ToArray();
}
}
So, although I'm using 2 streams, the first time returns the bytes and as such, I'm not using the same stream, I'm simply working with the bytes.

replace
using (var ms = new MemoryStream(img))
{
var pic = Image.FromStream(ms);
pic.Save(this._absolutePath, this._format);
}
to
var pic = Image.FromStream(new MemoryStream(img)));
pic.Save(this._absolutePath, this._format);
Check this Bitmap and Image constructor dependencies

Related

C# - Convert JBIG1 images to other image formats

I need to convert a JBIG1 image to another image format, such as JPEG or PNG, but I can't seem to find anything related to this.
This JBIG1 image is received encoded in Base64.
I've tried using System.Drawing in .NET to accomplish this, but a "System.ArgumentException: Parameter is not valid" exception is thrown on calling Image.FromStream() using the JBIG1 byte array data.
See code below:
byte[] binData = ConvertFromBase64StringToArray("BASE64 ENCODED JBIG1 IMAGE GOES HERE");
Image img = binData.ConvertToImage();
img.Save("C:/Images/converted-from-jbig.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Functions used:
public static byte[] ConvertFromBase64StringToArray(string base64String)
{
byte[] data = Convert.FromBase64String(base64String);
using (var stream = new MemoryStream(data, 0, data.Length))
{
data = stream.ToArray();
}
return data;
}
public static Image ConvertToImage(this byte[] byteArrayIn)
{
var ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); //exception thrown in this line
return returnImage;
}
Does anyone have any knowledge to share about this topic?
You'll probably need a third party library to work with JBig files. It looks like https://github.com/dlemstra/Magick.NET has support for that.

ExternalException when saving .bmp to MemoryStream as .png

I'm converting a batch of .bmp's to .png. This is the relevant part of the code:
foreach (string path in files) {
using (fs = new FileStream(path, FileMode.Open)) bmp = new Bitmap(fs);
using (ms = new MemoryStream()) {
bmp.Save(ms, ImageFormat.Png);
bmp.Dispose();
png = Image.FromStream(ms);
}
png.Save(path);
}
At the line bmp.Save(ms, ImageFormat.Png); the following exception is thrown:
System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'
According to MSDN this means the image was either saved with the wrong format or to the same location it was read from. The latter is not the case. However, I don't see how I gave it the wrong format: on the same MSDN page an example is given where a .bmp is converted to a .gif in the same manner.
Does it have to do with me saving to a MemoryStream? This is done so that I can overwrite the original file with the converted one. (Note that the .bmp suffix is kept intentionally. This shouldn't be the problem, since the exception appears before the final file is saved.)
In the MSDN documentation of that Bitmap constructor it says:
You must keep the stream open for the lifetime of the Bitmap.
and that same remark can be found on Image.FromStream.
So your code should carefully handle the scope and lifetime of the streams it uses for each of those bitmaps/images.
Combining all that the following code handles those streams correctly:
foreach (string path in files) {
using (var ms = new MemoryStream()) //keep stream around
{
using (var fs = new FileStream(path, FileMode.Open)) // keep file around
{
// create and save bitmap to memorystream
using(var bmp = new Bitmap(fs))
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
}
}
// write the PNG back to the same file from the memorystream
using(var png = Image.FromStream(ms))
{
png.Save(path);
}
}
}

Convert Base64 String To Image Attachment

I have an image converted to a base64 string that I need to convert back to an image and attach to a MailMessage.
Here is the relevant code converting it from base64 string to image (I think I can skip the Image object and do this using one memory stream, but had some issues implementing that). Attempting to save the Image to a MemoryStream throws a generic GDI+ error:
Image image = ImageHelper.Base64ToImage(attachment.FieldData);
if (image != null)
{
using (var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png); // Throws a generic GDI+ error on Save
ms.Position = 0;
var imageAttachment = new Attachment(ms, "image.png", "image/png");
message.Attachments.Add(imageAttachment);
}
}
public static class ImageHelper
{
public static Image Base64ToImage(string base64String)
{
if (string.IsNullOrEmpty(base64String))
{
return null;
}
byte[] imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
}
}
I'm able to serve up the raw base64 string elsewhere using an img tag and it works fine so I'm confident that the problem isn't with the base64 string itself:
<img src="data:image/png;base64,<myBase64StringHere>" alt="My Image" width="500" />
I must be doing something wrong in converting it back, but I haven't been able to figure out the issue. Thanks for any help with this!
Image.FromStream(Stream) says, "You must keep the stream open for the lifetime of the Image", but your using statement disposes the stream as the Image is returned. A workaround would be to return both the image and the stream together as a tuple and without the using:
public static Tuple<Image, MemoryStream> Base64ToImage(string base64String)
{
if (string.IsNullOrEmpty(base64String))
{
return null;
}
byte[] imageBytes = Convert.FromBase64String(base64String);
var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return new Tuple<Image, MemoryStream>(image, ms);
}
Also note to take care and view each overload on the MSDN pages. Normally I would say, "view the most encompassing overload to get all the remarks and notes", but in this case that is not true. The MSDN page for the biggest overload, Image.FromStream Method (Stream, Boolean, Boolean) does not mention that you need to keep the stream open, but I am fairly certain that is a mistake on that particular page.

argumentnullexception bitmap save to memorystream

I'm trying to save my Bitmap to MemoryStream - what wrong in this code? Why it gets me argumentnullexception ??
private void insertBarCodesToPDF(Bitmap barcode)
{
.......
MemoryStream ms = new MemoryStream();
barcode.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp); //<----
byte [] qwe = ms.ToArray();
.......
}
UPD: StackTrace
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
in WordTest.FormTestWord.insertBarCodesToPDF(Bitmap barcode)
I believe that your problem is related to the type of image you are trying to save to the MemoryStream as. According to this Code Project article: Dynamically Generating Icons (safely), some of the ImageFormat types do not have the necessary encoder to allow the Save function to save as that type.
I ran the following to determine which types did and didn't work:
System.Drawing.Bitmap b = new Bitmap(10, 10);
foreach (ImageFormat format in new ImageFormat[]{
ImageFormat.Bmp,
ImageFormat.Emf,
ImageFormat.Exif,
ImageFormat.Gif,
ImageFormat.Icon,
ImageFormat.Jpeg,
ImageFormat.MemoryBmp,
ImageFormat.Png,
ImageFormat.Tiff,
ImageFormat.Wmf})
{
Console.Write("Trying {0}:", format);
MemoryStream ms = new MemoryStream();
bool success = true;
try
{
b.Save(ms, format);
}
catch (Exception)
{
success = false;
}
Console.WriteLine("\t{0}", (success ? "works" : "fails"));
}
This gave the results of:
Trying Bmp: works
Trying Emf: fails
Trying Exif: fails
Trying Gif: works
Trying Icon: fails
Trying Jpeg: works
Trying MemoryBMP: fails
Trying Png: works
Trying Tiff: works
Trying Wmf: fails
There is a Microsoft KB Article which states that some of the ImageFormat types are read-only.

Image.Save(..) throws a GDI+ exception because the memory stream is closed

i've got some binary data which i want to save as an image. When i try to save the image, it throws an exception if the memory stream used to create the image, was closed before the save. The reason i do this is because i'm dynamically creating images and as such .. i need to use a memory stream.
this is the code:
[TestMethod]
public void TestMethod1()
{
// Grab the binary data.
byte[] data = File.ReadAllBytes("Chick.jpg");
// Read in the data but do not close, before using the stream.
Stream originalBinaryDataStream = new MemoryStream(data);
Bitmap image = new Bitmap(originalBinaryDataStream);
image.Save(#"c:\test.jpg");
originalBinaryDataStream.Dispose();
// Now lets use a nice dispose, etc...
Bitmap2 image2;
using (Stream originalBinaryDataStream2 = new MemoryStream(data))
{
image2 = new Bitmap(originalBinaryDataStream2);
}
image2.Save(#"C:\temp\pewpew.jpg"); // This throws the GDI+ exception.
}
Does anyone have any suggestions to how i could save an image with the stream closed? I cannot rely on the developers to remember to close the stream after the image is saved. In fact, the developer would have NO IDEA that the image was generated using a memory stream (because it happens in some other code, elsewhere).
I'm really confused :(
As it's a MemoryStream, you really don't need to close the stream - nothing bad will happen if you don't, although obviously it's good practice to dispose anything that's disposable anyway. (See this question for more on this.)
However, you should be disposing the Bitmap - and that will close the stream for you. Basically once you give the Bitmap constructor a stream, it "owns" the stream and you shouldn't close it. As the docs for that constructor say:
You must keep the stream open for the
lifetime of the Bitmap.
I can't find any docs promising to close the stream when you dispose the bitmap, but you should be able to verify that fairly easily.
A generic error occurred in GDI+.
May also result from incorrect save path!
Took me half a day to notice that.
So make sure that you have double checked the path to save the image as well.
Perhaps it is worth mentioning that if the C:\Temp directory does not exist, it will also throw this exception even if your stream is still existent.
Copy the Bitmap. You have to keep the stream open for the lifetime of the bitmap.
When drawing an image: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI
public static Image ToImage(this byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
using (var image = Image.FromStream(stream, false, true))
{
return new Bitmap(image);
}
}
[Test]
public void ShouldCreateImageThatCanBeSavedWithoutOpenStream()
{
var imageBytes = File.ReadAllBytes("bitmap.bmp");
var image = imageBytes.ToImage();
image.Save("output.bmp");
}
I had the same problem but actually the cause was that the application didn't have permission to save files on C. When I changed to "D:\.." the picture has been saved.
You can try to create another copy of bitmap:
using (var memoryStream = new MemoryStream())
{
// write to memory stream here
memoryStream.Position = 0;
using (var bitmap = new Bitmap(memoryStream))
{
var bitmap2 = new Bitmap(bitmap);
return bitmap2;
}
}
This error occurred to me when I was trying from Citrix. The image folder was set to C:\ in the server, for which I do not have privilege. Once the image folder was moved to a shared drive, the error was gone.
A generic error occurred in GDI+. It can occur because of image storing paths issues,I got this error because my storing path is too long, I fixed this by first storing the image in a shortest path and move it to the correct location with long path handling techniques.
I was getting this error, because the automated test I was executing, was trying to store snapshots into a folder that didn't exist. After I created the folder, the error resolved
One strange solution which made my code to work.
Open the image in paint and save it as a new file with same format(.jpg). Now try with this new file and it works. It clearly explains you that the file might be corrupted in someway.
This can help only if your code has every other bugs fixed
It has also appeared with me when I was trying to save an image into path
C:\Program Files (x86)\some_directory
and the .exe wasn't executed to run as administrator, I hope this may help someone who has same issue too.
For me the code below crashed with A generic error occurred in GDI+on the line which Saves to a MemoryStream. The code was running on a web server and I resolved it by stopping and starting the Application Pool that was running the site.
Must have been some internal error in GDI+
private static string GetThumbnailImageAsBase64String(string path)
{
if (path == null || !File.Exists(path))
{
var log = ContainerResolver.Container.GetInstance<ILog>();
log.Info($"No file was found at path: {path}");
return null;
}
var width = LibraryItemFileSettings.Instance.ThumbnailImageWidth;
using (var image = Image.FromFile(path))
{
using (var thumbnail = image.GetThumbnailImage(width, width * image.Height / image.Width, null, IntPtr.Zero))
{
using (var memoryStream = new MemoryStream())
{
thumbnail.Save(memoryStream, ImageFormat.Png); // <= crash here
var bytes = new byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, bytes.Length);
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
}
}
}
I came across this error when I was trying a simple image editing in a WPF app.
Setting an Image element's Source to the bitmap prevents file saving.
Even setting Source=null doesn't seem to release the file.
Now I just never use the image as the Source of Image element, so I can overwrite after editing!
EDIT
After hearing about the CacheOption property(Thanks to #Nyerguds) I found the solution:
So instead of using the Bitmap constructor I must set the Uri after setting CacheOption BitmapCacheOption.OnLoad.(Image1 below is the Wpf Image element)
Instead of
Image1.Source = new BitmapImage(new Uri(filepath));
Use:
var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filepath);
image.EndInit();
Image1.Source = image;
See this: WPF Image Caching
Try this code:
static void Main(string[] args)
{
byte[] data = null;
string fullPath = #"c:\testimage.jpg";
using (MemoryStream ms = new MemoryStream())
using (Bitmap tmp = (Bitmap)Bitmap.FromFile(fullPath))
using (Bitmap bm = new Bitmap(tmp))
{
bm.SetResolution(96, 96);
using (EncoderParameters eps = new EncoderParameters(1))
{
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bm.Save(ms, GetEncoderInfo("image/jpeg"), eps);
}
data = ms.ToArray();
}
File.WriteAllBytes(fullPath, data);
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; ++j)
{
if (String.Equals(encoders[j].MimeType, mimeType, StringComparison.InvariantCultureIgnoreCase))
return encoders[j];
}
return null;
}
I used imageprocessor to resize images and one day I got "A generic error occurred in GDI+" exception.
After looked up a while I tried to recycle the application pool and bingo it works. So I note it here, hope it help ;)
Cheers
I was getting this error today on a server when the same code worked fine locally and on our DEV server but not on PRODUCTION. Rebooting the server resolved it.
public static byte[] SetImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
public static Bitmap SetByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}

Categories

Resources