如何在WPF中的矩形中绘制字符串 [英] How to draw String in a rectange in WPF

查看:108
本文介绍了如何在WPF中的矩形中绘制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

h1,

我需要将字符串放在wpf.DrawingContext类中的矩形内has
方法drawImage(ImageSource,Rect).如何将字符串转换为图像,以便
我可以使用drawimage()来绘制带有rectangle的矩形/或者是否有其他替代方法
我需要将WPf转换为xpsdocument.

h1,

i need to place string inside a rectangle in wpf.DrawingContext class has
a method drawImage(ImageSource,Rect).how can i convert string to image so that
i can use drawimage() to draw rectangle with sting/or is there an other alternative
I need to convert WPf to xpsdocument.

   DrawingVisual visual = new DrawingVisual();
        {
            DrawingContext dc = visual.RenderOpen();
            Pen bluePen = new Pen(Brushes.Blue, 1);
            dc.DrawRectangle(Brushes.Yellow, bluePen, new Rect(10, 10, 800, 1000));
            Brush pinkBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0,255));
           ds.DrawImage();
            dc.Close();
      XpsDocument doc = new XpsDocument(PackageName,FileAccess.ReadWrite);
      XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
      writer.Write(visual);
        }

DocumentViewer.Document=doc ;//report viewer



问候,
sajith



Regards,
sajith

推荐答案

System.Windows.Media.DrawingContext.DrawText有什么问题?

请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.media.drawingcontext.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.drawtext.aspx [ ^ ].

—SA
And what''s wrong with System.Windows.Media.DrawingContext.DrawText?

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.drawtext.aspx[^].

—SA


您可以通过FlowDocument实现此目的:

You could achieve this through the FlowDocument:

<FlowDocumentReader>
    <FlowDocument

        Name="flowDocument"

        ColumnWidth="400" FontSize="14" FontFamily="Georgia">
        <Paragraph>
            <Grid>
                <Rectangle Fill="Red" Width="200" Height="100"/>
                <TextBlock Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </Paragraph>
    </FlowDocument>
</FlowDocumentReader>



后面的代码将如下所示:



The code behind would then look like:

public static void SaveAsXps(string path, FlowDocument document)
{
    using (var package = Package.Open(path, FileMode.Create))
    {
        using (var xpsDocument = new XpsDocument(package, System.IO.Packaging.CompressionOption.Maximum))
        {
            var xpsSerializationManager = new XpsSerializationManager(new XpsPackagingPolicy(xpsDocument), false);
            var documentPaginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
            xpsSerializationManager.SaveAsXaml(documentPaginator);
        }
    }
}



和调用代码:

SaveAsXps("temp.xps",flowDocument);

只是另一种方式.我个人更喜欢将UI保留在XAML中.
您需要为此添加对ReachFramework.dll的引用.



And the calling code:

SaveAsXps("temp.xps", flowDocument);

Just another way to do it. I personally prefer keeping the UI in XAML.
You''ll need to add a reference to ReachFramework.dll for this to work.


为什么不使用 TextBlock.TextWrapping [ ^ ]设置为true.

看看 VisualsToXpsDocument [
Why not use a TextBlock[^].

Remember to set the TextBlock.TextWrapping[^] to true.

Have a look at VisualsToXpsDocument[^], it provides methods for writing visual objects to xps documents.

Best regards
Espen Harlinn


这篇关于如何在WPF中的矩形中绘制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆