Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;


	/// <summary>
	/// Description of ImageConverter.
	/// </summary>
	public class ImageConverter
	{
		public ImageConverter()
		{
		}
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            using (var ms = new MemoryStream())
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //right here
                return ms.ToArray();
            }
        }


What I have tried:

I dont really know why it throws that exception. Please help me guys
Posted
Updated 21-Sep-16 22:14pm
Comments
InbarBarkai 22-Sep-16 4:10am    
Is imageIn null?
Member 12724527 22-Sep-16 4:35am    
Hey, I think you're right. I think I forgot to set a default Image. Silly me. Thanks bro

try like this, and check VS console for exception:
C#
public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
           if (imageIn!=null){
            using (var ms = new MemoryStream())
            {
                try{
                  imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //right here
                
                  return ms.ToArray();
                 }
                 catch(Exception ex){
                 Console.WriteLine("See me:"+ex.Message);
                  }
            }
           } else return null;
        }
 
Share this answer
 
Comments
Member 12724527 22-Sep-16 4:32am    
Thanks bro you're super awesome
Possibly because imageIn doesn't contain a reference to a valid object.
You should post error message full details in order to get better help.
 
Share this answer
 
Comments
Member 12724527 22-Sep-16 4:33am    
That's all that i have noticed I probably missed something.

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



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