Displaying binary images from database is now easy. C1BinaryImage for ASP.NET Wijmo brings you the power to show binary image from database or stream in your application. You can use it in any data bound control (Repeater, DataList, GridView etc.) to display images which originate from binary image field in the data source. In this tutorial you will learn how to :
As always, the first step is to create a web application. Refer to our Lets Get Started section in our blog.
You can use C1BinaryImage to display image from a local path using the following steps :
You can also specify image from a url in the C1BinaryImage. Simply set the ImageUrl property to the image url as given below:
<cc1:C1BinaryImage ID="C1BinaryImage1" runat="server" ImageUrl="http://www.componentone.com/newimages/Products/Visuals/se_theming.png"/>
The C1BinaryImage control provides following image settings options:
<cc1:C1BinaryImage ID="C1BinaryImage1" runat="server" ImageUrl="~/Koala.png" Height="100px" ResizeMode="Fit" Width="100px" />
There are three Resize Modes:
Tooltip - Adds tooltip text to the control, that appears when you hover the mouse over the binary image.Following are 2 simple steps to show tooltip :
<cc1:C1BinaryImage ID="C1BinaryImage1" runat="server" ImageUrl="~/C1.png" <strong>SavedImageName</strong>="ComponentOne" />
C1BinaryImage gives you control over the rendered image with customhttphandler. You can customize the image as per your requirements at run time. Following steps explains how to add a watermark to the image being rendered in C1BinaryImage
Override the ProcessImageData method to customize the image:
public class CustomBinaryImageHandler : C1BinaryImageHandler
{
public override C1BinaryImageData ProcessImageData(C1BinaryImageData imageData)
{
using (var outStream = new System.IO.MemoryStream())
using (var inStream = new System.IO.MemoryStream(imageData.Data))
using (var image = Bitmap.FromStream(inStream))
{
var newImage = AddWatermark(image);
newImage.Save(outStream, ImageFormat.Png);
imageData.Data = outStream.ToArray();
imageData.MimeType = "image/png";
imageData.ImageFileName += "_Watermark";
}
return base.ProcessImageData(imageData);
}
private Image AddWatermark(Image image)
{
var watermarkString = "ComponentOne";
var font = new Font("Arial", 8F, FontStyle.Regular);
var newImage = new Bitmap(image.Width, image.Height);
using (var g = Graphics.FromImage(newImage))
{
g.DrawImage(image, new Point(0, 0));
g.DrawString(watermarkString, font, new SolidBrush(Color.White), new PointF(2, newImage.Height - font.Height - 2));
}
return newImage;
}
}
Save this file as CustomBinaryImageHandler.ashx.
For demos on C1BinaryImage, check out our ControlExplorer Demo.