如何在Xamarin Imageview中显示MySQL BLOB图像?
问题描述:
我已经将我的imageview转换为位图,然后转换为字节数组。如何在Xamarin Imageview中显示MySQL BLOB图像?
Bitmap bmp = ((BitmapDrawable)ivTest.Drawable).Bitmap;
MemoryStream stream = new MemoryStream();
bmp.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] bitmapData = stream.ToArray();
我想要做的是获取此BLOB图像并将其显示在另一个ImageView中。我可以通过使用SQL查询来做到这一点吗?
答
下面是一些代码,我已经写在C#这样做(这是从Web API方法)
public void viewLatestImageUpload(int imageType)
{
Context.Response.ContentType = "image/jpeg";
Stream strm = ShowAlbumImage(imageType);
byte[] buffer = new byte[8192];
int byteSeq = strm.Read(buffer, 0, 8192);
while (byteSeq > 0)
{
Context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 8192);
}
}
下面是从数据库中拉图像的方法:
public Stream ShowAlbumImage(int imageType)
{
SqlConnection myConnection = getEtechConnection();
string sql;
if (imageType == 1)
{
sql = "SELECT TOP 1 Img_stream from EquipmentImages ORDER BY CreatedOn DESC";
}
else
{
sql = "SELECT TOP 1 Receipt from ETechReceiptImg ORDER BY StrtDate DESC, StrtTime DESC";
}
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.CommandType = CommandType.Text;
myConnection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
myConnection.Close();
}
}
谢谢为了这。这可以转移到Xamarin和MySQL和PHPMyAdmin使用 –
我会这样认为。一些SQL可能需要进行调整,因为语法略有不同。至少这应该给你一些工作。它会加载到要显示它的页面上下文中。 –