Click here to Skip to main content
15,916,846 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to convert my string value to BitmapImage or ImageSource.

I have tried this by converting the string into stream and then convert into bitmap image. but that value is null. Can any one help to get the image for equivalent string?
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jun-15 1:57am    
Even though it's possible to represent images as strings, it does not seem to make any practical sense, except some special cases such as sending images in e-mails, and the like. I doubt that you have such cases. Most likely, you don't understand what you really want. But if in fact you do, you have to explain it.
—SA
jdeep84 19-Jun-15 2:03am    
when you say string.., are you referring to path of some image? if so some value converters can be used/implemented.

If you mean convert your string to some image with that string on it ,it would be like this >>
C#
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

string textPrntMag1 = "Shivanand";
string textPrntMag2 = "Nagarabetta";
string textPrntMag3 = "All is well";
string textPrntMag4 = "R u usman_k";
Bitmap bitmap = new Bitmap(1, 1);
Font font = new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = 220;
int height = 150;

bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

graphics.DrawString(textPrntMag1, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 20);
graphics.DrawString(textPrntMag4, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 40);
graphics.DrawString(textPrntMag2, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 60);
graphics.DrawString(textPrntMag3, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 90);
graphics.Flush();
graphics.Dispose();

string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
bitmap.Save(Server.MapPath("~/images/ShipmentOrders/") + fileName, ImageFormat.Jpeg);


If you mean an image from Base64 encoding:

Convert Image to Base64 String and Base64 String to Image
Or you can see this post.
 
Share this answer
 
public static BitmapImage Base64StringToBitmap(string base64String)
{
byte[] byteBuffer = Convert.FromBase64String(base64String);
MemoryStream memoryStream = new MemoryStream(byteBuffer);
memoryStream.Position = 0;

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(memoryStream);

memoryStream.Close();
memoryStream = null;
byteBuffer = null;

return bitmapImage;
}
 
Share this answer
 
v3

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