如何在Xamarin Imageview中显示MySQL BLOB图像?

时间:2017-04-11 15:17:54

标签: android mysql xamarin bitmap imageview

我已将imageview转换为位图,然后转换为字节数组。

Bitmap bmp = ((BitmapDrawable)ivTest.Drawable).Bitmap;

            MemoryStream stream = new MemoryStream();
            bmp.Compress(Bitmap.CompressFormat.Png, 0, stream);
            byte[] bitmapData = stream.ToArray();

我想要做的是获取此BLOB图像并将其显示在另一个ImageView中。我可以使用SQL查询吗?

1 个答案:

答案 0 :(得分:0)

以下是我用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();
        }
    }