// 图片转换成二进制
public static byte[] ImageDatabytes(string FilePath) { if (!File.Exists(FilePath)) return null; Bitmap myBitmap = new Bitmap(Image.FromFile(FilePath)); using (MemoryStream curImageStream = new MemoryStream()) { myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png); curImageStream.Flush(); byte[] bmpBytes = curImageStream.ToArray(); //如果转字符串的话 //string BmpStr = Convert.ToBase64String(bmpBytes); return bmpBytes; } }
//二进制转化图片
public static Bitmap GetImage(byte[] ImageDatas) { try { //如果是字符串的话 //byte[] resultBytes = Convert.FromBase64String(ImageDatas); using (MemoryStream ImageMS = new MemoryStream()) { ImageMS.Write(ImageDatas, 0, ImageDatas.Length); Bitmap resultBitmap = new Bitmap(ImageMS); return resultBitmap; } } catch { return null; } }
二进制图片上传
public class ImageUploader { private static readonly HttpClient client = new HttpClient(); public async Task UploadImageAsync(string imagePath, string uploadUrl) { // 将图片转换为二进制 byte[] imageBytes = File.ReadAllBytes(imagePath); // 使用HttpClient上传二进制数据 ByteArrayContent content = new ByteArrayContent(imageBytes); var response = await client.PostAsync(uploadUrl, content); // 检查响应状态 response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } // 使用示例 class Program { static async Task Main(string[] args) { ImageUploader uploader = new ImageUploader(); string imagePath = "path/to/your/image.jpg"; string uploadUrl = "http://yourserver.com/upload"; await uploader.UploadImageAsync(imagePath, uploadUrl); } }
标签:return,string,二进制,Bitmap,转化,new,byte,图片 From: https://www.cnblogs.com/MaoZhuaShi/p/18636349