Occurs when a request is made to draw an item in an owner-drawn RasterImageList.
public event EventHandler<RasterImageListDrawItemEventArgs> DrawItem Public Event DrawItem As EventHandler(Of RasterImageListDrawItemEventArgs)
public:event EventHandler<RasterImageListDrawItemEventArgs^>^ DrawItem
The event handler receives an argument of type RasterImageListDrawItemEventArgs containing data related to this event. The following RasterImageListDrawItemEventArgs properties provide information specific to this event.
| Property | Description |
|---|---|
| Graphics | The Graphics object into which the item should be drawn. |
| Item | The RasterImageListItem to draw. |
When you set the ViewStyle property to RasterImageListViewStyle.OwnerDraw, the RasterImageList control will fire the DrawItem event for each event that needs to be drawn. The RasterImageListDrawItemEventArgs argument passed to a DrawItem event handler provides a System.Drawing.Graphics object that enables you to perform drawing and other graphical operations on the surface of the RasterImageList along with the RasterImageListItem object that needs to be drawn.
This example creates an owner drawn RasterImageList.
using Leadtools.WinForms;using Leadtools;using Leadtools.Codecs;using Leadtools.Drawing;class MyForm3 : Form{RasterImageList imageList;RasterCodecs codecs;public MyForm3(string title){Text = title;// Set the size of the formSize = new Size(400, 200);// Create a new RasterImageList controlimageList = new RasterImageList();imageList.Dock = DockStyle.Fill;imageList.SelectionMode = RasterImageListSelectionMode.Single;imageList.Size = Size;Controls.Add(imageList);imageList.BringToFront();codecs = new RasterCodecs();// Create three itemsstring imagesPath = LEAD_VARS.ImagesDir;for (int i = 0; i < 3; i++){// Load the imageint index = i + 1;string imageFileName = Path.Combine(imagesPath, @"ImageProcessingDemo\Image" + index.ToString() + ".cmp");RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);RasterImageListItem item = new RasterImageListItem(image, 1, "Item" + index.ToString());// Select the first itemif (i == 0)item.Selected = true;// Add the item to the image listimageList.Items.Add(item);}// Change the item sizeimageList.ItemSize = new Size(200, 200);// Change the item image sizeimageList.ItemImageSize = new Size(120, 120);// We are going to draw the items ourselvesimageList.ViewStyle = RasterImageListViewStyle.OwnerDraw;// Add a handler to the DrawItem eventimageList.DrawItem += new EventHandler<RasterImageListDrawItemEventArgs>(rasterImageList_DrawItem);// Add the RasterImageList to the control collection.Controls.Add(imageList);}private void rasterImageList_DrawItem(object sender, RasterImageListDrawItemEventArgs e){RasterImageListItem item = e.Item;RasterImageList imageList = item.ImageList;Graphics g = e.Graphics;// get the item rectangleRectangle rc = imageList.GetItemRectangle(item);// sanity checkif (rc.IsEmpty)return;// we want to draw a 1 pixel black rectangle around the item// then we fill the inside of the rectangle with white if the item// is not selected or lightgray if it isg.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1);// we used up 1 pixelrc.Inflate(-1, -1);Brush b;if (item.Selected)b = Brushes.LightGray;elseb = Brushes.White;g.FillRectangle(b, rc);// calculate the rectangles for image and textif (imageList.ShowItemText){// text is visible// draw the text at the bottom of the itemint textHeight = (int)(g.MeasureString("WWW", imageList.Font).Height + 4);Rectangle textRect = Rectangle.FromLTRB(rc.Left,rc.Bottom - textHeight,rc.Right,rc.Bottom);if (!textRect.IsEmpty){StringFormat sf = new StringFormat();sf.Alignment = StringAlignment.Center;sf.LineAlignment = StringAlignment.Center;sf.Trimming = StringTrimming.EllipsisPath;sf.FormatFlags = StringFormatFlags.NoWrap;g.DrawString(item.Text,imageList.Font,Brushes.Black,textRect,sf);sf.Dispose();// we need to update the item rectangle for the space// we used up to draw the textrc.Height -= textRect.Height;}}// rc is the image rectangleif (!rc.IsEmpty){// now rc holds the rectangle to draw the image into// first, set the correct pageint savePage = -1;if (item.Image.Page != item.Page){// the page is different// save current image page so we can set it back when we are donesavePage = item.Image.Page;// disable the image events, we are going to set the page back,// so we do not want anybody subscribing to this image Changed// event to know we changed it.item.Image.DisableEvents();// set new pageitem.Image.Page = item.Page;}try{// we want to center the image into whatever left of rcSize itemImageSize = imageList.ItemImageSize;Rectangle imageRect = new Rectangle(rc.Left + (rc.Width - itemImageSize.Width) / 2,rc.Top + (rc.Height - itemImageSize.Height) / 2,itemImageSize.Width,itemImageSize.Height);// we want to keep the aspect ratioimageRect = RasterImageList.GetFixedAspectRatioImageRectangle(item.Image.ImageWidth,item.Image.ImageHeight,imageRect);// draw the imageLeadRect lRect = new LeadRect(imageRect.Left, imageRect.Top, imageRect.Width, imageRect.Height);RasterImagePainter.Paint(item.Image, e.Graphics, lRect, imageList.PaintProperties);// finally, draw a black rectangle around the imageimageRect.Inflate(1, 1);g.DrawRectangle(Pens.Black,imageRect.Left,imageRect.Top,imageRect.Width - 1,imageRect.Height - 1);}finally{// reset the old pageif (savePage != -1){item.Image.Page = savePage;// re-enable the eventsitem.Image.EnableEvents();}}}}}public void RasterImageList_DrawItem(string title){MyForm3 form = new MyForm3(title);form.ShowDialog();}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";}
Imports Leadtools.WinFormsImports LeadtoolsImports Leadtools.CodecsImports Leadtools.DrawingPrivate Class MyForm3 : Inherits FormPrivate imageList As RasterImageListPrivate codecs As RasterCodecsPublic Sub New(ByVal title As String)Text = title' Set the size of the formSize = New Size(400, 200)' Create a new RasterImageList controlimageList = New RasterImageList()imageList.Dock = DockStyle.FillimageList.SelectionMode = RasterImageListSelectionMode.SingleimageList.Size = SizeControls.Add(imageList)imageList.BringToFront()codecs = New RasterCodecs()' Create three itemsDim imagesPath As String = LEAD_VARS.ImagesDirFor i As Integer = 0 To 2' Load the imageDim index As Integer = i + 1Dim imageFileName As String = Path.Combine(imagesPath, "ImageProcessingDemo\Image" & index.ToString() & ".cmp")Dim image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)Dim item As RasterImageListItem = New RasterImageListItem(image, 1, "Item" & index.ToString())' Select the first itemIf i = 0 Thenitem.Selected = TrueEnd If' Add the item to the image listimageList.Items.Add(item)Next i' Change the item sizeimageList.ItemSize = New Size(200, 200)' Change the item image sizeimageList.ItemImageSize = New Size(120, 120)' We are going to draw the items ourselvesimageList.ViewStyle = RasterImageListViewStyle.OwnerDraw' Add a handler to the DrawItem eventAddHandler imageList.DrawItem, AddressOf rasterImageList_DrawItem' Add the RasterImageList to the control collection.Controls.Add(imageList)End SubPrivate Sub rasterImageList_DrawItem(ByVal sender As Object, ByVal e As RasterImageListDrawItemEventArgs)Dim item As RasterImageListItem = e.ItemDim imageList As RasterImageList = item.ImageListDim g As Graphics = e.Graphics' get the item rectangleDim rc As Rectangle = imageList.GetItemRectangle(item)' sanity checkIf rc.IsEmpty ThenReturnEnd If' we want to draw a 1 pixel black rectangle around the item' then we fill the inside of the rectangle with white if the item' is not selected or lightgray if it isg.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1)' we used up 1 pixelrc.Inflate(-1, -1)Dim b As BrushIf item.Selected Thenb = Brushes.LightGrayElseb = Brushes.WhiteEnd Ifg.FillRectangle(b, rc)' calculate the rectangles for image and textIf imageList.ShowItemText Then' text is visible' draw the text at the bottom of the itemDim textHeight As Integer = CInt(g.MeasureString("WWW", imageList.Font).Height + 4)Dim textRect As Rectangle = Rectangle.FromLTRB(rc.Left, rc.Bottom - textHeight, rc.Right, rc.Bottom)If (Not textRect.IsEmpty) ThenDim sf As StringFormat = New StringFormat()sf.Alignment = StringAlignment.Centersf.LineAlignment = StringAlignment.Centersf.Trimming = StringTrimming.EllipsisPathsf.FormatFlags = StringFormatFlags.NoWrapg.DrawString(item.Text, imageList.Font, Brushes.Black, textRect, sf)sf.Dispose()' we need to update the item rectangle for the space' we used up to draw the textrc.Height -= textRect.HeightEnd IfEnd If' rc is the image rectangleIf (Not rc.IsEmpty) Then' now rc holds the rectangle to draw the image into' first, set the correct pageDim savePage As Integer = -1If item.Image.Page <> item.Page Then' the page is different' save current image page so we can set it back when we are donesavePage = item.Image.Page' disable the image events, we are going to set the page back,' so we do not want anybody subscribing to this image Changed' event to know we changed it.item.Image.DisableEvents()' set new pageitem.Image.Page = item.PageEnd IfTry' we want to center the image into whatever left of rcDim itemImageSize As Size = imageList.ItemImageSizeDim imageRect As Rectangle = New Rectangle(rc.Left + (rc.Width - itemImageSize.Width) \ 2, rc.Top + (rc.Height - itemImageSize.Height) \ 2,itemImageSize.Width, itemImageSize.Height)' we want to keep the aspect ratioimageRect = RasterImageList.GetFixedAspectRatioImageRectangle(item.Image.ImageWidth, item.Image.ImageHeight, imageRect)' draw the imageDim lRect As LeadRect = New LeadRect(imageRect.Left, imageRect.Top, imageRect.Width, imageRect.Height)RasterImagePainter.Paint(item.Image, e.Graphics, lRect, imageList.PaintProperties)' finally, draw a black rectangle around the imageimageRect.Inflate(1, 1)g.DrawRectangle(Pens.Black, imageRect.Left, imageRect.Top, imageRect.Width - 1, imageRect.Height - 1)Finally' reset the old pageIf savePage <> -1 Thenitem.Image.Page = savePage' re-enable the eventsitem.Image.EnableEvents()End IfEnd TryEnd IfEnd SubEnd ClassPublic Sub RasterImageList_DrawItem(ByVal title As String)Dim form As MyForm3 = New MyForm3(title)form.ShowDialog()End SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
