如何将字符串转换为BimapImage [英] How to convert string to BimapImage

查看:63
本文介绍了如何将字符串转换为BimapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的字符串值转换为BitmapImage或ImageSource。



我通过将字符串转换为流然后转换为位图图像来尝试此操作。但该值为空。任何人都可以帮助获取等效字符串的图像吗?

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?

推荐答案

如果你的意思是将你的字符串转换成一个带有该字符串的图像,它就像这样> ;>

If you mean convert your string to some image with that string on it ,it would be like this >>
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);





如果你的意思是来自Base64编码的图像:



将图像转换为Base64字符串和Base64字符串到图像

或你可以看到这个发布


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;



返回bitmapImage;

}
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;
}


这篇关于如何将字符串转换为BimapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆