【问题标题】:How to Show a Binary Data in an Image如何在图像中显示二进制数据
【发布时间】:2013-07-13 04:45:08
【问题描述】:

实际上,我已将<Binary data> 保存在数据库中:

HttpPostedFile PostedFile = Request.Files[m + 2];

byte[] fileData = null;
 using (var binaryReader = new BinaryReader(Request.Files[m + 2].InputStream))
 {
 fileData = binaryReader.ReadBytes(Request.Files[m + 2].ContentLength);
 }

现在,我想在图像中显示<Binary data>。 我使用了一些代码,例如:

ASPxImage objimg = new ASPxImage();
objimg.ID = "objimg" + (j + 1);
objimg.Width = 100;
objimg.Height = 100;
byte[] buffer = null;
buffer = (byte[])DtChoices.Rows[j]["COLUMN_IMAGE"];
MemoryStream ms = new MemoryStream(buffer);
objimg.Value = System.Drawing.Image.FromStream(ms);

但是,我无法显示图片。谁能帮帮我?

【问题讨论】:

    标签: c# .net image binary-data


    【解决方案1】:

    我没有使用 DevExpress 控件库,但是从文档中我可以收集到正确的类来执行此操作是 ASPxBinaryImage。他们的网站上有一个示例,地址为http://www.devexpress.com/Support/Center/Example/Details/E1414

    您的控制 -

    <dxe:ASPxBinaryImage ID="ASPxBinaryImage1" runat="server" Value='<%# ConvertOleObjectToByteArray(Eval("Image")) %>'></dxe:ASPxBinaryImage>
    

    转换函数-

    public partial class _Default : System.Web.UI.Page {
        const string BITMAP_ID_BLOCK = "BM";
        const string JPG_ID_BLOCK = "\u00FF\u00D8\u00FF";
        const string PNG_ID_BLOCK = "\u0089PNG\r\n\u001a\n";
        const string GIF_ID_BLOCK = "GIF8";
        const string TIFF_ID_BLOCK = "II*\u0000";
        const int DEFAULT_OLEHEADERSIZE = 78;
        public static byte[] ConvertOleObjectToByteArray(object content) {
            if (content != null && !(content is DBNull)) {
                byte[] oleFieldBytes = (byte[])content;
                byte[] imageBytes = null;
                // Get a UTF7 Encoded string version
                Encoding u8 = Encoding.UTF7;
                string strTemp = u8.GetString(oleFieldBytes);
                // Get the first 300 characters from the string
                string strVTemp = strTemp.Substring(0, 300);
                // Search for the block
                int iPos = -1;
                if (strVTemp.IndexOf(BITMAP_ID_BLOCK) != -1) {
                    iPos = strVTemp.IndexOf(BITMAP_ID_BLOCK);
                } else if (strVTemp.IndexOf(JPG_ID_BLOCK) != -1) {
                    iPos = strVTemp.IndexOf(JPG_ID_BLOCK);
                } else if (strVTemp.IndexOf(PNG_ID_BLOCK) != -1) {
                    iPos = strVTemp.IndexOf(PNG_ID_BLOCK);
                } else if (strVTemp.IndexOf(GIF_ID_BLOCK) != -1) {
                    iPos = strVTemp.IndexOf(GIF_ID_BLOCK);
                } else if (strVTemp.IndexOf(TIFF_ID_BLOCK) != -1) {
                    iPos = strVTemp.IndexOf(TIFF_ID_BLOCK);
                }
                // From the position above get the new image
                if (iPos == -1) {
                    iPos = DEFAULT_OLEHEADERSIZE;
                }
                //Array.Copy(
                imageBytes = new byte[oleFieldBytes.LongLength - iPos];
                MemoryStream ms = new MemoryStream();
                ms.Write(oleFieldBytes, iPos, oleFieldBytes.Length - iPos);
                imageBytes = ms.ToArray();
                ms.Close();
                ms.Dispose();
                return imageBytes;
            }
            return null;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2017-07-12
      • 1970-01-01
      • 2020-08-03
      • 2013-02-01
      • 2014-08-09
      • 2013-07-26
      • 2013-12-22
      • 1970-01-01
      相关资源
      最近更新 更多