Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting "Value cannot be null.\r\nParameter name: encoder" error while saving a Bitmap image using RawFormat.
Sample code:
C#
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var image = new System.Drawing.Bitmap(500, 400);
                var stream = new MemoryStream();
                image.Save(stream, ImageFormat.Bmp);
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
            }
        }
    }


The RawFormat doesn't exist in the existing list of ImageEncoders as below code returns null.
C#
var imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(codec => codec.FormatID == image.RawFormat.Guid);

Note: The image could be any type(JPEG, BMP, PNG) etc. Image.Save() should work on image.RawFormat.
RawFormat is not Bitmap type. If I Change image.RawFormat to ImageFormat.Bmp, the save operation succeeds.

Referred below links but found nothing for making it independent of image type.
http://stackoverflow.com/questions/9073619/image-save-crashing-value-cannot-be-null-r-nparameter-name-encoder
Any suggestions are welcome.
Posted

Not easy to help without more details.

A few checkpoints which I would investigate:

- what kind of raw format (sony raw image, canon, ...)
- Are you using wpf? It uses more framework build-in drawing options like the BitmapDecoder
- WIC code pack installed?

http://www.obviousidea.com/windows-software/wic/[^]

Example code I would try with:

C#
BitmapDecoder bitmapDecoder = BitmapDecoder.Create(new Uri(sourceFilePath),
BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
BitmapEncoder bitmapEncoder = new BmpBitmapEncoder();
bitmapEncoder.Frames.Add(bitmapDecoder .Frames[0]);
Stream memoryStream = new MemoryStream();
bmpEnc.Save(memoryStream );
Image srcImage = Bitmap.FromStream(memoryStream);


May it'll help you. Regards
 
Share this answer
 
v2
You are using the Image.Save[^] method with Stream and ImageFormat parameters.

Image.RawFormat[^] is not a value it is a property that holds the image format.

The image format can only be one of the values as described in the documentation for the System.Drawing.ImageFormat property[^].

There are other variations of the Image.Save [^] method that will accept and call custom encoders.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900