How to add a 'System.Drawing.Image' to a 'System.Web.UI.WebControls.Image'

六眼飞鱼酱① 提交于 2019-12-11 03:52:18

问题


I have two functions:

Function 1: ImageToByteArray: Is used to Convert an Image into a Byte Array and then Store in an Oracle Database, in a BLOB Field.

            public byte[] ImageToByteArray(string sPath)
            {
                byte[] data = null;
                FileInfo fInfo = new FileInfo(sPath);
                long numBytes = fInfo.Length;
                FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fStream);
                data = br.ReadBytes((int)numBytes);
                return data;
            }

Function 2: ByteArrayToImage: Is used to Convert a Byte Array from the Database into an Image:

           public System.Drawing.Image ByteArrayToImage(byte[] byteArray)
            {
                MemoryStream img = new MemoryStream(byteArray);
                System.Drawing.Image returnImage = System.Drawing.Image.FromStream(img);
                return returnImage;
            }

In my Markup I have an Imgage Control: <asp:Image ID="Image1" runat="server" />

In the Code Behind I want to Assign the Returned Value from Function 2 to (which is of type System.Drawing.Image) to the "image1" control which is of type (System.Web.UI.WebControls.Image).

Obviously I can't just assign: image1 = ByteArrayToImage(byteArray); because I'd get the following error: Cannot implicitly convert type 'System.Drawing.Image' to 'System.Web.UI.WebControls.Image'

Is there anyway to do this?


回答1:


You can't. WebControls.Image is just a HTML container for an image url - you can't store the image data directly in it, you just store the reference (url) to an image file.

If you need to retrieve image data dynamically, the usual approach is to create an image handler that will handle the request and return the image as a stream that the browser can display.

See this question




回答2:


It looks like you just want a simple method to convert an image byte array into a picture. No problem. I found an article that helped me a lot.

    System.Web.UI.WebControls.Image image = (System.Web.UI.WebControls.Image)e.Item.FindControl("image");

    if (!String.IsNullOrEmpty(currentAd.PictureFileName))
    {
        image.ImageUrl = GetImage(currentAd.PictureFileContents);
    }
    else
    {
        image.Visible = false;
    }

    //The actual converting function
    public string GetImage(object img)
    {
        return "data:image/jpg;base64," + Convert.ToBase64String((byte[])img);
    }

PictureFileContents is a Byte[] and that's what the function GetImage is taking as an object.




回答3:


I think it is possible and I hope it be helpful. My solution for this is here:

public System.Web.UI.WebControls.Image HexStringToWebControlImage(string hexString)
{
    var imageAsString = HexString2Bytes(hexString);

    MemoryStream ms = new MemoryStream();
    ms.Write(imageAsString, 0, imageAsString.Length);

    if (imageAsString.Length > 0)
    {
        var base64Data = Convert.ToBase64String(ms.ToArray());
        return new System.Web.UI.WebControls.Image
        {
            ImageUrl = "data:image/jpg;base64," + base64Data
        };
    }
    else
    {
        return null;
    }
}

public byte[] HexString2Bytes(string hexString)
{
    int bytesCount = (hexString.Length) / 2;
    byte[] bytes = new byte[bytesCount];
    for (int x = 0; x < bytesCount; ++x)
    {
        bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
    }

    return bytes;
}


来源:https://stackoverflow.com/questions/10590055/how-to-add-a-system-drawing-image-to-a-system-web-ui-webcontrols-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!