Telerik Forums
UI for ASP.NET AJAX Forum
1 answer
156 views

I've been trying to display a System.Drawing.Image (obtained from live camera feed snapshot) dynamically in a DataList control with asp:Image control but not having any success.

I was looking at RadBinaryImage to see if this is a viable option.

The images to be displayed are variable (aka dynamic number of images not static number/count) so I may have 5 images or 10 images or 7 images that are all System.Drawing.Image types.  I want to display those images in a Grid or DataList.


    <asp:DataList ID="cameras" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="Table">
        <ItemTemplate>
            <asp:Image ID="cameraX" runat="server" />
        </ItemTemplate>
    </asp:DataList> 

 

In the ItemDataBound event handler I convert to byte array and assign to asp:Image ImageUrl ... but this results in nothing being displayed.  No errors, just nothing displayed.  Would RadBinaryImage be a solution?
        protected void cameras_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            
            Models.Facility.Camera camera = e.Item.DataItem as Models.Facility.Camera;
            Image img = GetImage(camera);
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            System.Web.UI.WebControls.Image imgControl = (System.Web.UI.WebControls.Image)e.Item.FindControl("camera");
            var base64Data = Convert.ToBase64String(ms.ToArray());
            imgControl.ImageUrl = "data:image/jpg;base64," + base64Data;

        }
Cheers, Rob.

 

Rob
Top achievements
Rank 2
Iron
Iron
 answered on 17 Jan 2023
1 answer
320 views

I have a RadGrid that displays some images from a database.  I'm trying to add a button to the grid that allows the user to download the image using the original file name and extension saved in the ImageName column.

 


<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSourceImageUpload" OnItemCommand="RadGrid1_ItemCommand"> <MasterTableView DataKeyNames="ImageID" DataSourceID="SqlDataSourceImageUpload" > <Columns> <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="download_file" UniqueName="download" ></telerik:GridButtonColumn> <telerik:GridBoundColumn DataField="ImageName" UniqueName="ImageName" ></telerik:GridBoundColumn> <telerik:GridBinaryImageColumn DataField="ImageData" ></telerik:GridBinaryImageColumn> </Columns> </MasterTableView>

 


                <asp:SqlDataSource ID="SqlDataSourceImageUpload" runat="server"
                                   ConnectionString="<%$ ConnectionStrings:123ConnectionString %>" 
                                   SelectCommand="SELECT * FROM [tbl_Images]" 
                                   DeleteCommand="DELETE FROM [tbl_Images] WHERE [ImageID] = @ImageID" 
                                   InsertCommand="INSERT INTO [tbl_Images] ([ImageData], [ImageName], [Text]) 
                                   VALUES (@ImageData, @ImageName, @Text)" 
                                   UpdateCommand="UPDATE [tbl_Images] SET [Text] = @Text WHERE [ImageID] = @ImageID">
                    <DeleteParameters>
                        <asp:Parameter Name="ImageID" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:QueryStringParameter Name="IncidentID" QueryStringField="ID" Type="String" />
                        <asp:Parameter Name="Text" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Text" Type="String" />
                        <asp:Parameter Name="ImageID" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>

I found this code online, and honestly not sure how to make it all work with what I'm trying to do.  I really feel like I'm over complicating this whole thing.  Is there an easy button I'm missing somewhere?

The ultimate goal is for the user to click a download button and have the image download using the ImageName field as the file name.

    Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
        If e.CommandName = "download_file" Then
            Dim ditem As GridDataItem = CType(e.Item, GridDataItem)
            Dim filename As String = ditem("ImageName").Text
            Dim path As String = MapPath("/sample/" & filename)
            Dim bts As Byte() = System.IO.File.ReadAllBytes(path)
            Response.Clear()
            Response.ClearHeaders()
            Response.AddHeader("Content-Type", "Application/octet-stream")
            Response.AddHeader("Content-Length", bts.Length.ToString())
            Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
            Response.BinaryWrite(bts)
            Response.Flush()
            Response.[End]()
        End If
    End Sub


Attila Antal
Telerik team
 answered on 05 Oct 2022
1 answer
140 views

I have a RadBinaryImage in a RadGrid template column and I'm trying to account for situations where the value is null. In reviewing some of the posts, I saw the following solution.


<telerik:RadBinaryImage runat="server" ID="RadBinaryImage2" DataValue='<%# Eval("vSignature") Is DBNull ? null  Eval("vSignature") %>' AutoAdjustImageControlSize="false" Height="100px" Width="400px" ToolTip="Signature" AlternateText="Signature" />

However, I get a design-time error stating that...

DBNull is a class type and cannot be used as an expression.

I have seen examples of this, but this does not work for me in Visual Studio 2017, Asp.Net 4.6.

Any help on this is greatly appreciated!

Attila Antal
Telerik team
 answered on 13 Aug 2021
Narrow your results
Selected tags
Tags
+? more
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?