Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am converting a string( div html content) in image. i am getting an error '
Parameter is not valid.
'

What I have tried:

string str = "<div>This is a test Message</div>";

       byte[] byt = System.Text.Encoding.UTF8.GetBytes(str);

       base64String = Convert.ToBase64String(byt);

       Base64ToImage(base64String).Save(Server.MapPath("~/images/Hello.jpg"));



public System.Drawing.Image Base64ToImage(string base64String)
   {
       // Convert Base64 String to byte[]
       byte[] imageBytes = Convert.FromBase64String(base64String);
       MemoryStream ms = new MemoryStream(imageBytes, 0,
         imageBytes.Length);

       // Convert byte[] to Image
       ms.Write(imageBytes, 0, imageBytes.Length);
       System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
       return image;
   }
Posted
Updated 9-Oct-19 22:10pm

string str = "This is a\r\n test Message";

// create a dummy Bitmap just to get the Graphics object
Bitmap img = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(img);

// The font for our text
Font f = new Font("Arial", 14);

// work out how big the text will be when drawn as an image
SizeF size = g.MeasureString(str, f);

// create a new Bitmap of the required size
img = new Bitmap((int)Math.Ceiling(size.Width), (int)Math.Ceiling(size.Height));
g = Graphics.FromImage(img);

// give it a white background
g.Clear(Color.White);

// draw the text in black
g.DrawString(str, f, Brushes.Black, 0, 0);

// save the image
img.Save(@"c:\temp\Hello.jpg");
 
Share this answer
 
Images cannot be created for just any old string: they are structured files which contain a lot of "packing information" which relates to the actual image data, but isn't the actual pixels.

You can't just assume that any string translated to Base64 will be a valid image: it won't! And when it isn't, you get a "parameter not valid" error because the data it expects and needs to "decode" the image data into an actual picture you can display just isn't there!

Try reading an image file from disk as a byte array, convert that to Base64, then process that to a image.
 
Share this answer
 
Comments
Vikram Singh Rathaur 29-Jun-17 6:26am    
My requirement is to send a image(which we want to convert from an html) over email
OriginalGriff 29-Jun-17 6:37am    
Why not just attach the image file?
If you can't do that, then read it as a byte array, convert it to Base64, and sand it that way. Exactly how you send it depends on what you want to do with it when the email arrives, but a basic HTML embed would be an img tag:

img alt="My Image" src="data:image/jpeg;base64,...Base64 Data..."
Vikram Singh Rathaur 25-Nov-19 7:00am    
some time it is not automatically download in mailbox

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