相关疑难解决方法(0)

将位图转换为字节数组

使用C#,有没有更好的方式来在Windows转换Bitmap到一个byte[]比保存到临时文件和读取使用的结果FileStream

c# bitmap

216
推荐指数
9
解决办法
34万
查看次数

将文件转换为Base64String并再次返回

标题说明了一切:

  1. 我在这样的tar.gz档案中读到了
  2. 将文件分成一个字节数组
  3. 将这些字节转换为Base64字符串
  4. 将该Base64字符串转换回字节数组
  5. 将这些字节写回新的tar.gz文件

我可以确认这两个文件大小相同(以下方法返回true)但我无法再提取副本版本.

我错过了什么吗?

Boolean MyMethod(){
    using (StreamReader sr = new StreamReader("C:\...\file.tar.gz")) {
        String AsString = sr.ReadToEnd();
        byte[] AsBytes = new byte[AsString.Length];
        Buffer.BlockCopy(AsString.ToCharArray(), 0, AsBytes, 0, AsBytes.Length);
        String AsBase64String = Convert.ToBase64String(AsBytes);

        byte[] tempBytes = Convert.FromBase64String(AsBase64String);
        File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes);
    }
    FileInfo orig = new FileInfo("C:\...\file.tar.gz");
    FileInfo copy = new FileInfo("C:\...\file_copy.tar.gz");
    // Confirm that both original and copy file have the same number of bytes
    return (orig.Length) == (copy.Length);
}
Run Code Online (Sandbox Code Playgroud)

编辑:工作示例更简单(感谢@TS):

Boolean MyMethod(){
    byte[] AsBytes = File.ReadAllBytes(@"C:\...\file.tar.gz");
    String AsBase64String …
Run Code Online (Sandbox Code Playgroud)

c# base64 system.io.fileinfo streamreader

94
推荐指数
1
解决办法
15万
查看次数

将图像路径转换为base64字符串

如何在C#中将图像转换为base64字符串?

例如,我有图像的路径,C:/image/1.gif并希望data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..返回.

c# base64 image

93
推荐指数
7
解决办法
17万
查看次数

标签 统计

c# ×3

base64 ×2

bitmap ×1

image ×1

streamreader ×1

system.io.fileinfo ×1