Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
   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


Regards,
sajith
Posted

 
Share this answer
 
Comments
Espen Harlinn 24-Jan-12 10:26am    
5'ed - it will get the job done as long as OP is willing to handle wrapping himself :)
Sergey Alexandrovich Kryukov 24-Jan-12 10:44am    
Thank you, Espen.
--SA
sajithnet 29-Jan-12 1:35am    
many thanks SAKryukov .i tried with DrawText but my need is to create the
report dynamically .while drawing text i need to create a rectangle and place
the Text inside the rectangle.
please check the link .here i have given the sample report format.
http://social.expression.microsoft.com/Forums/en-US/wpf/thread/dbbcdbf6-4fe8-4ead-9436-03105b0308d7[^]
You could achieve this through the FlowDocument:

XML
<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:
C#
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);
        }
    }
}


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.
 
Share this answer
 
Comments
sajithnet 9-Feb-12 10:44am    
sorry for late replay.ur post helps me a lot thank u very much
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900