图像字节数组转换成String类型

File file = new File("C:/abc.png");

InputStream is = new FileInputStream(file);

byte[] byte = new byte[200000];

is.read(byte);

BASE64Encoder  encoder = new BASE64Encoder();

String str=  encoder.encode(b);
BASE64Decoder base = new BASE64Decoder();
byte[] c =base.decodeBuffer(str);

转载于:https://www.cnblogs.com/lqfalya/archive/2010/05/25/1743312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C# 中,你可以使用 `Bitmap` 和 `Graphics` 类来将字符串转换为指定大小的像素字节数组。 以下是一个示例代码,演示如何将字符串转换为指定大小(宽度和高度)的像素字节数组: ```csharp using System; using System.Drawing; using System.Drawing.Imaging; public class Program { public static void Main(string[] args) { string text = "Hello, World!"; int width = 200; // 指定图像宽度 int height = 50; // 指定图像高度 // 创建一个位图对象 Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); // 创建一个 Graphics 对象 using (Graphics graphics = Graphics.FromImage(bitmap)) { // 设置绘制文字的字体和颜色 Font font = new Font("Arial", 12, FontStyle.Regular); Brush brush = Brushes.Black; // 绘制文字在位图上 graphics.DrawString(text, font, brush, new PointF(0, 0)); } // 将位图转换成像素字节数组 byte[] pixelBytes; using (var stream = new System.IO.MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); pixelBytes = stream.ToArray(); } // 输出像素字节数组的长度 Console.WriteLine("Pixel Bytes Length: " + pixelBytes.Length); } } ``` 在上述代码中,我们首先指定了要转换的字符串 `text`,以及目标图像的宽度和高度。然后,我们创建了一个指定大小的位图对象 `bitmap`。接下来,我们使用 `Graphics` 对象在位图上绘制指定的字符串。最后,我们将位图保存为 PNG 格式的图像,并将其转换为像素字节数组 `pixelBytes`。 确保根据实际需求修改字符串内容、图像大小和保存格式等参数。你可以通过访问 `pixelBytes.Length` 获取像素字节数组的长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值