wpf 在不同DPI下如何在DrawingVisual中画出清晰的图形
2021-03-21 21:23
标签:分享 ted 100% block style span map idt 图片 100%即为96DPI; 125%即为120DPI; 150%即为144DPI; 175%即为168DPI. 运行结果:线是清晰锐利的 接下来把系统DPI改为150%,仍然是要求在相同位置画出一条一个像素宽的清晰横线. 运行结果:线依然是是清晰锐利的 wpf 在不同DPI下如何在DrawingVisual中画出清晰的图形 标签:分享 ted 100% block style span map idt 图片 原文地址:https://www.cnblogs.com/8u7tgyjire7890/p/9502991.html
Dim RenderTargetBitmap As New RenderTargetBitmap(1920, 1080, 144, 144,PixelFormats.Pbgra32)
RenderTargetBitmap.Render(drawingVisual)
Public Class Window1
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
Dim Thickness As Double = 1 ‘线宽1个单位,此处等同于1个像素
Dim HalfThickness As Double = Thickness * 0.5
drawingContext.DrawLine(New Pen(Brushes.Black, Thickness),
New Point(0, 100 - HalfThickness),
New Point(1920, 100 - HalfThickness)
)
drawingContext.Close()
Dim RenderTargetBitmap As New RenderTargetBitmap(1920, 1080, 96, 96, PixelFormats.Pbgra32) ‘注意这里创建的是96DPI的图像对象
RenderTargetBitmap.Render(drawingVisual)
Dim image As New Image
image.Source = RenderTargetBitmap
Canvas1.Children.Add(image)
End Sub
End Class
Window
x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp3"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="300"
Height="300"
mc:Ignorable="d" Loaded="Window_Loaded">
Canvas x:Name="Canvas1">
Canvas>
Window>
Public Class Window1
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
Dim Thickness As Double = 1 / 1.5 ‘在144(150%)DPI的情况下,线宽1个像素
Dim HalfThickness As Double = Thickness * 0.5
drawingContext.DrawLine(New Pen(Brushes.Black, Thickness),
New Point(0, 100 / 1.5 - HalfThickness),
New Point(1920, 100 / 1.5 - HalfThickness)
)
drawingContext.Close()
Dim RenderTargetBitmap As New RenderTargetBitmap(1920, 1080, 144, 144, PixelFormats.Pbgra32) ‘注意这里创建的是144DPI的图像对象
RenderTargetBitmap.Render(drawingVisual)
Dim image As New Image
image.Source = RenderTargetBitmap
Canvas1.Children.Add(image)
End Sub
End Class
文章标题:wpf 在不同DPI下如何在DrawingVisual中画出清晰的图形
文章链接:http://soscw.com/essay/67319.html