You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#将Quickbooks POS返回的Base64编码图片转Byte[]并保存JPG

Fixing QuickBooks POS Base64 Image Saving to JPG

Alright, let's get this sorted out. The core issue here is that you're using ASCII encoding to convert a Base64 string, which is the wrong approach entirely. Base64 has its own dedicated decoding logic that ASCII can't handle, and that's why your output JPG file is unreadable.

The Correct Solution

Instead of rolling your own conversion with ASCII, use .NET's built-in Convert.FromBase64String method—it's purpose-built for decoding Base64 data into binary:

// Extract the value from the <EncodedPicture> element in your XML response
string imageBase64 = "/9j/4AAQSkZJRgABAgEAAAAAAAD..."; // Replace with your full Base64 string

// Convert Base64 directly to a valid byte array
byte[] imageBytes = Convert.FromBase64String(imageBase64);

// Save the byte array as a JPG file
File.WriteAllBytes(@"C:\Temp\image.jpg", imageBytes);

Why Your Original Code Failed

  • ASCII encoding maps each character to a single byte, but Base64 works by grouping 4 characters to decode into 3 bytes of binary data. Using ASCII completely ignores this structure, corrupting the binary data that forms the JPG.
  • Even though most Base64 characters fall within the ASCII range, special characters like +, /, and = have specific meanings in Base64 that ASCII doesn't account for. This corruption results in a file that can't be recognized as a valid image.

Quick Check of Your Base64 Data

I verified the Base64 snippet you provided—it starts with /9j/4AAQSkZJRgAB, which is the standard Base64 header for JPG files. Using the code above will correctly decode this into a usable image file.

内容的提问来源于stack exchange,提问作者B. Abdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:18:08