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;
}
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
Is there a recommended way to use the RadBinaryImage control to serve an image via CSS?
Ideally, I would like to replace a line like this:
background-image: url("mybackground.gif");
with something like this:
background-image: url("<%= RadBinaryImage1.UrlTheBrowserRequests %>");
<telerik:RadBinaryImage ID="RadBinaryImage1" runat="server" />
...but as far as I can tell, the serving url not exposed. Is the url is deterministic enough to recreate?
I'm open to alternatives, too. The best I've come up with so far is styling the image as a relatively positioned layer, but there are significant complexities and limitations to that approach, and any explicit sizing plays poorly with the serverside size that get rendered by the control.
Thanks,
Phil
I did the two latest upgrades and I cannot compile my project now. Every Telerik reference gives "The type or namespace name 'Telerik' could not be found"
I am always nervous when I do Telerik upgrades.
I emptied the licenses.licx file, which is frequently the problem.
In the Webconfig
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Telerik.Web.UI" publicKeyToken="121FAE78165BA3D4" />
<bindingRedirect oldVersion="1.0.0.0-2018.3.909.45" newVersion="2020.1.114.45" />
</dependentAssembly>
<dependentAssembly>
The version is now 2022.1.19.45. Why does the upgrade not set the correct settings?
Should it be
<dependentAssembly>like all the other dependenticies?
This change gives the same errors. I assume the publicKeyToken is unchanged.
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!
Hi Guys,
I need to generate a default value for a BinaryImage (in a RadListView) when the image source from the database is NULL.
This is the BinaryImage code in RadListView
<
asp:PlaceHolder
ID
=
"badgeContainer"
runat
=
"server"
></
asp:PlaceHolder
>
</
LayoutTemplate
>
<
ItemTemplate
>
<
asp:LinkButton
ID
=
"lb1"
runat
=
"server"
CommandName
=
"content"
CommandArgument='<%#Eval("bid") %>'>
<
telerik:RadBinaryImage
ID
=
"rbImageBadge"
runat
=
"server"
DataValue='<%# IIf(Eval("badge") IsNot DBNull.Value, Eval("badge"), New System.Byte(-1) {})%>'
AutoAdjustImageControlSize="false" Width="120px" Height="120px" ToolTip='<%#Eval("badgetitle", "{0}") %>'
AlternateText='<%#Eval("badgetitle", "{0}") %>' />
</
asp:LinkButton
>
I have tried everything that I have read but just cannot get it to show a default image.
All help appreciated!
My Handler is
Imports System.Data.SqlClient
Public Class badgeImage : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim conn As SqlConnection = Nothing
conn = New SqlConnection(ConfigurationManager.ConnectionStrings("jbdata").ConnectionString)
context.Response.ContentType = "image/png"
Dim id As Integer = Convert.ToInt32(context.Request.QueryString("ID"))
Dim cmdText As String = "select badge from badges where bid=" & id
Dim command As New SqlCommand(cmdText, conn)
conn.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
If id > 0 Then
Dim image As Byte() = DirectCast(reader(0), Byte())
context.Response.BinaryWrite(image)
Else
Dim location As String = System.Web.HttpContext.Current.Server.MapPath("~/badgz/images/default.png")
System.Web.HttpContext.Current.Response.WriteFile(location)
End If
reader.Close()
conn.Close()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Having a bit of trouble with the resize mode on the BinaryImage. I have specified the Width and Height in the aspx attributes and set the ResizeMode to fit, however it's just setting every image to the sizes that I specified rather than fitting them as it should be. This results in images being stretched rather than fit as if it's actually set to fill instead.
I'm using an httphandler to serve the image from a database and this is the relevant code:
Is there something that I'm missing here? Simply setting the ResizeMode to fit and setting a max height and width should be enough, right?
<
asp:Panel
runat
=
"server"
Style
=
"text-align:center;"
>
<
telerik:RadBinaryImage
runat
=
"server"
ID
=
"imgStu"
ResizeMode
=
"Fit"
Width
=
"1050"
Height
=
"700"
/>
</
asp:Panel
>
imgStu.ImageUrl =
"~/frm/ShowImage.ashx?id="
+ strImageID;