我正在使用WPF爲我的WinRT應用程序生成平鋪圖像。我有一個平鋪UserControl它轉換爲PNG,這效果很好(它確實,請不要告訴我,否則)。 WinRT通過我的規模屬性100%(320x150),140%(434x210)和180%(558x270),我用它來生成正確尺寸的圖像。可能使用DPI在WPF中複製WinRT像素密度更改
當我想在我的瓷磚中使用圖像。我在我的Tile UserControl的代碼背後複製了WinRT的圖像選擇功能(您可以提供圖像的比例和WinRT應用程序自動選擇正確的比例)。因此,根據比例尺我選擇更大或更小的圖像源。然而,在較大的尺寸上,我的字體大小保持不變,看起來非常小。我不認爲我需要根據比例尺來更改字體大小,因爲這不是在WinRT中發生的情況,所以它必須是我用來將UserControl轉換爲PNG和使用DPI的代碼。這裏是我的轉換代碼:
// I pass in 320x150 or 434x210 or 558x270 depending on the scale.
public static MemoryStream ToPng(
this FrameworkElement frameworkElement,
double width,
double height)
{
BitmapSource bitmapSource = ToBitmapSource(frameworkElement, width, height);
PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
pngBitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapSource));
MemoryStream memoryStream = new MemoryStream();
pngBitmapEncoder.Save(memoryStream);
memoryStream.Position = 0;
return memoryStream;
}
public static BitmapSource ToBitmapSource(
this FrameworkElement frameworkElement,
double width,
double height)
{
Size renderingSize = new Size(width, height);
frameworkElement.Measure(renderingSize);
Rect renderingRectangle = new Rect(new Point(0, 0), renderingSize);
frameworkElement.Arrange(renderingRectangle);
frameworkElement.UpdateLayout();
Rect bounds = VisualTreeHelper.GetDescendantBounds(frameworkElement);
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)frameworkElement.ActualWidth,
(int)frameworkElement.ActualHeight,
96,
96,
PixelFormats.Pbgra32);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(frameworkElement);
drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
}
renderBitmap.Render(drawingVisual);
return renderBitmap;
}
感謝您的任何幫助。非常感激。