/// <summary> /// 获取文件的缩略图 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public Image GetThumbNail(string fileName) { Bitmap bmp; try { bmp = (Bitmap)DrawingTool.GetImage(fileName); } catch { bmp = new Bitmap(ThumbNailSize, ThumbNailSize); //If we cant load the image, create a blank one with ThumbSize } bmp = (Bitmap)DrawingTool.GetScaleImage(bmp, ThumbNailSize, ThumbNailSize); if (bmp.Width < ThumbNailSize || bmp.Height < ThumbNailSize) { Bitmap bitmap2 = new Bitmap(ThumbNailSize, ThumbNailSize); Graphics g = Graphics.FromImage(bitmap2); Point point = new Point(); point.X = (ThumbNailSize - bmp.Width) / 2; point.Y = (ThumbNailSize - bmp.Height) / 2; g.DrawImage(bmp, point); g.Dispose(); bmp.Dispose(); return(bitmap2); } return(bmp); }
public void Paint(System.Drawing.Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle) { if (value == null) { return; } if ((value is Image) == false) { Debug.Assert(false, "value 不是 Image"); return; } Image image = (Image)value; bool scaleImage = false; if (image.Width > cellBounds.Width || image.Height > cellBounds.Height) { scaleImage = true; int imageWidth = cellBounds.Width - 2; int imageHeight = cellBounds.Height - 2; image = DrawingTool.GetScaleImage(image, imageWidth, imageHeight); } Point drawInPoint = new Point(cellBounds.X + cellBounds.Width / 2 - image.Width / 2, cellBounds.Y + cellBounds.Height / 2 - image.Height / 2); graphics.DrawImage(image, new Rectangle(drawInPoint, image.Size)); if (scaleImage) { image.Dispose(); } }
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); graphics.FillRectangle(Brushes.White, cellBounds.X + 1, cellBounds.Y + 1, cellBounds.Width - 2, cellBounds.Height - 2); int imageLocationX = cellBounds.X + 2; int imageLocationY = cellBounds.Y + 2; int imageSizeW = 16; int imageSizeH = 16; Rectangle imageBounds = new Rectangle(imageLocationX, imageLocationY, imageSizeW, imageSizeH); int textLocationX = imageLocationX + imageSizeW + 2; int textLocationY = cellBounds.Y + 4; Point textLoation = new Point(textLocationX, textLocationY); if (this.Value != null && this.Value.ToString() != String.Empty) { string resourceName = this.Value.ToString(); Image imageDraw; if (_resourceService.Container(resourceName)) { ImageResourceInfo imageResource = _resourceService.GetImageResource(resourceName); Image image = imageResource.GetImage(); imageDraw = DrawingTool.GetScaleImage(image, imageSizeW, imageSizeH); if (image != imageDraw) { image.Dispose(); } } else { imageDraw = DrawingTool.Mark.FileNotFind(imageBounds.Size); } try { graphics.DrawImage(imageDraw, imageBounds); } catch (Exception exception) { Debug.Assert(false, exception.Message); imageDraw = DrawingTool.Mark.FileNotFind(imageBounds.Size); graphics.DrawImage(imageDraw, imageBounds); } imageDraw.Dispose(); Font font = new Font(cellStyle.Font, cellStyle.Font.Style | FontStyle.Bold); Brush textBrush = new SolidBrush(cellStyle.ForeColor); graphics.DrawString(resourceName, font, textBrush, textLoation); font.Dispose(); textBrush.Dispose(); } }
internal override void DrawItemContent(Graphics g, Rectangle bounds, ImageListViewItem item) { if (_thumbnailSizeInited == false) { SizeF headerSize = g.MeasureString(item.Header, Theme.ItemHeaderFont); _headerHeight = (int)Math.Ceiling(headerSize.Height); int width = LayoutManager.ItemSize.Width - _itemPadding.Width * 2; int height = LayoutManager.ItemSize.Height - _itemPadding.Height * 3 - _headerHeight; _thumbnailSize = new Size(width, height); _thumbnailSizeInited = true; } Image img = null; if (_thumbnailsCache.Container(item)) { img = _thumbnailsCache.GetThumbnail(item); } else { img = DrawingTool.GetScaleImage(item.Image, _thumbnailSize); _thumbnailsCache.AddThumbnail(item, img); } if (img != null) { Rectangle pos = DrawingTool.GetSizedImageBounds(img, new Rectangle(bounds.Location + _itemPadding, _thumbnailSize)); g.DrawImage(img, pos); if (Math.Min(pos.Width, pos.Height) > 32) { using (Pen pOuterBorder = new Pen(Theme.ImageOuterBorderColor)) { g.DrawRectangle(pOuterBorder, pos); } if (System.Math.Min(_thumbnailSize.Width, _thumbnailSize.Height) > 32) { using (Pen pInnerBorder = new Pen(Theme.ImageInnerBorderColor)) { g.DrawRectangle(pInnerBorder, Rectangle.Inflate(pos, -1, -1)); } } } } _headerBounds = new Rectangle(); _headerBounds.X = _itemPadding.Width; _headerBounds.Y = LayoutManager.ItemSize.Height - _headerHeight - _itemPadding.Height; _headerBounds.Width = _thumbnailSize.Width; _headerBounds.Height = _headerHeight; _headerBounds.Offset(bounds.Location); using (SolidBrush brush = new SolidBrush(Theme.ItemHeaderColor)) { g.DrawString(item.Header, Theme.ItemHeaderFont, brush, _headerBounds, _itemHeaderStringFormat); } }