Based on the documentation I've seen, the Avatar control is looking for a url for the image. Is there a way to display a binary image from a sql datasource instead?
1 Answer, 1 is accepted
0
Rumen
Telerik team
answered on 02 Feb 2024, 12:12 PM
Hi Angela,
Yes, you can display a binary image from a SQL datasource in the RadAvatar for ASP.NET AJAX control, but not directly. Since the RadAvatar control expects a URL to an image, you would need to serve your binary image data through a handler or a similar endpoint that can convert the binary data into a stream that the browser can then interpret as an image. Here’s an example with dummy binary data for an image (in a real scenario, fetch from your database):
Implement a generic handler (ImageHandler.ashx)
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
publicclassImageHandler : IHttpHandler {
publicvoidProcessRequest(HttpContext context) {
// Dummy binary data for an image, in a real scenario, fetch from your databasebyte[] imageData = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAB40lEQVR4nO2T21EEMQwEr8g/DeI8PigVLMXB+qkZuTsBS2714wEAAAAAAAAAMJ3n++OZPcMM3rIHgCslhEQdFSopIaQS9kJ+VuFeib2QalgLeVWDcyXWQipiK+S/ClwrsRVSFUshd6/fsRJLIZWxE9J69W6V2AmpjpWQ3mt3qsRKyAnYCBm9cpdKbIScgoWQWdftUImFkJOQFzL7qtUrkRdyGtJCVl2zciXSQk5EVsjqK1atRFbIqUgK2XW9ipVICjkZOSG7r1atEjkhpyMlJOtalSqREgJCQrKvNPv9QEYIfCIhROU6FeaQEAJfpAtRuMrvZM+TLgSupArJvsZXZM5FIWKk\r\n"); // A small orange triangle
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(imageData);
}
publicbool IsReusable {
get {
returntrue;
}
}
}
Configure RadAvatar (Default.aspx) - Instead of providing a direct URL to an image file, you would set the Image property of the RadAvatar to the URL of your handler.