C#如何从字节数组中获取图像(blob转换为字节数组)? [英] C# How to get image from array of bytes (blob converted into array of bytes)?

查看:497
本文介绍了C#如何从字节数组中获取图像(blob转换为字节数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将图片从Android应用程序保存到MySQL斑点。现在我们想用C#获取该图像。有谁能建议C#的等效代码?我们想要显示
原始图片。我已成功从数据库中检索数据。它返回了我的字节数组。我已经尝试了很多方法,但我得到的图像体积小,质量低



< pre class ="lang-cs prettyprint"style ="padding:5px; border-width:0px; border-style:initial; border-color:initial; font-size:13px; line-height:inherit; font-family: Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif; vertical-align:baseline; width:auto; max-height:600px; background-color:#eff0f1; color:#393318; word-wrap:normal"> public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte [] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes,Base64.DEFAULT)
return encodedImage;



}




解决方案

如果是图像则可以使用
Image
将字节数组转换为Winforms的可显示图像。如果您只是拥有字节数组,则将其放入
MemoryStream
然后使用
FromStream
阅读它。如果它已经在磁盘上,那么使用
FromFile
代替。您应该阅读该类型支持的图像格式的文档。它是有限的。如果它不支持您需要的格式,那么您将不得不使用第三方库。


We are saving a picture from android app to MySQL blob. Now we want to get that image in C#. Can anyone suggest a equivalent code for C#? We want to show original image. I have successfully retrieved data from database. it is returning me array of bytes.I have tried lots of methods but I am getting image with small size and with low quality

public String getStringImage(Bitmap bmp){
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
   byte[] imageBytes= baos.toByteArray();
   String encodedImage= Base64.encodeToString(imageBytes, Base64.DEFAULT)
   return encodedImage;

}


解决方案

If it is an image then you can use Image to convert the byte array to the displayable image, for Winforms. If you just have the byte array then put it into a MemoryStream and then use FromStream to read it. If it is on disk already then use FromFile instead. You should read the documentation on what image formats the type supports. It is limited. If it doesn't support the format you need then you'll have to use a third party library.


这篇关于C#如何从字节数组中获取图像(blob转换为字节数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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