protected override Size MeasureCore(Size availableSize) { if (!_dpi.HasValue) { _dpi = DpiUtil.GetDpi(this); } var drawing = (DoubleUtil.LessThanOrClose(_dpi.Value.X, 96) || HDPIDrawing == null) ? Drawing : HDPIDrawing; if (drawing != null && !drawing.Bounds.IsEmpty) { var bounds = drawing.Bounds; return(new Size(Math.Min(availableSize.Width, bounds.Width), Math.Min(availableSize.Height, bounds.Height))); } return(base.MeasureCore(availableSize)); }
protected override void OnRender(DrawingContext drawingContext) { if (!_dpi.HasValue) { _dpi = DpiUtil.GetDpi(this); } if (Drawing == null || HDPIDrawing == null) { drawingContext.DrawDrawing(null); return; } if (DoubleUtil.LessThanOrClose(_dpi.Value.X, 96) || HDPIDrawing == null) { RenderOptions.SetEdgeMode(this, EdgeMode.Aliased); drawingContext.DrawDrawing(Drawing); } else { RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified); drawingContext.DrawDrawing(HDPIDrawing); } }
private void Redraw() { if (_pointsDic.Count == 0) { _image.Source = null; _writeableBitmap = null; return; } //在不初始化 _imageGrid.Width 的前提下,每次生成 WriteableBitmap 都会导致 _imageGrid 的 ActualWidth 自动增大 2 像素左右 //暂未找到原因,故通过赋初值的方式解决 if (DoubleUtil.IsNaN(_imageGrid.Width) || DoubleUtil.IsZero(_imageGrid.Width)) { _imageGrid.Width = GetCurrentWidth(); } //当最大化窗体时后,此时,_scrollviewer.ViewportWidth 的值仍然为最大化之前的值,但 _scrollviewer.ActualWidth 已经是变更后的值,且有可能会大于 _scrollviewer.ViewportWidth //导致只能显示 _scrollviewer.ViewportWidth 大小区域的波形,故通过重置 _imageGrid.Width,间接变更 _scrollviewer.ViewportWidth,与 _scrollviewer.ActualWidth 保持一致 if (_scrollviewer != null && DoubleUtil.GreaterThan(_scrollviewer.ActualWidth, _scrollviewer.ViewportWidth)) { _imageGrid.Width = _scrollviewer.ActualWidth; } if (!_dpi.HasValue) { _dpi = DpiUtil.GetDpi(this); } var width = (int)GetCurrentWidth(); var height = (int)_imageGrid.ActualHeight; _writeableBitmap = new WriteableBitmap(width, height, _dpi.Value.X, _dpi.Value.Y, SWM.PixelFormats.Pbgra32, null); _writeableBitmap.Lock(); using (var backBitmap = new Bitmap(width, height, _writeableBitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, _writeableBitmap.BackBuffer)) { using (var graphics = Graphics.FromImage(backBitmap)) { graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.Clear(Color.Transparent); DrawBgLine(graphics, width); DrawX(graphics, width); foreach (var kvp in _pointsDic) { DrawLine(kvp.Value, graphics, width); } graphics.Flush(); } } GC.Collect(); _writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); _writeableBitmap.Unlock(); _image.Source = _writeableBitmap; }