Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Printing silverlight control in silverlight 2.0 RSS

3 replies

Last post May 21, 2009 02:22 PM by EliteMike

(0)
  • kondamca

    kondamca

    Member

    27 Points

    99 Posts

    Printing silverlight control in silverlight 2.0

    May 20, 2009 04:37 PM | LINK

    Hi All,

    I have silverlight control like employeedetails.xaml . In this file I have a button print. when I click the  print button the content in this control needs to be print. How can I get this in silverlight 2.0 . my application is complet silverlight light application only one aspx page is there to host silverlight application.

    Can some one please guide me how to get the print functionality in silverlight2.0 

    Thanks in advance.

    Regards,

    Konda.

     

    c# silverlight fullscreen c# silverlight deployment custom controls Silverlightlight Page Class Library Silverlight 2.0 silverlight 2.0 firefox linux 3d vista datagrid Silverlight 2.0 Java Applet Flash RIA Silverlight 2.0 client-side local data store performance security silverlight 2.0 security "native code" applications Silverlight 2 Asp.NET Futures Silverlight 2.0 silverligherLight 2 Beta 1 "Silverlight 2" Silverlight 2.0 Beta 1 Silverligh 2.0 silverligh 2.0 RaiseEvent Silverligh 2 "Silverlight 2 JSON" youtube Silverlight 2.0 Beta Silverlight 2.0 Secure Webservices SSL Silverlight 2.0 Secure Web Service SSL OnLoad Silverlight2 Scriptable Silverligh 2 Bet Silverlight 2.0 Beta silverlight2 JScript indexer Silverligh 2 Beta Silverlighlight 2.0 asp.NETsilverlight 2.0 Printing Silverlight 2 Beta 2 silverlight 2 Beta 1 WCF Xml XmlElement Silverlight 2.0 Beta 2 Silverlight Beta 2 silverligh 2 Beta 1 silverlight 2.0 Beta 2 silverlight 2 Beta 1 Silverligh 2 Beta 2 Silverlight 2.0 Beta 2 problem silverlighlight 2 Beta 2 silverlighlight Navigate from one xaml Silverlight 2 binding data grid CodePlex Silverlight2 Beta2 DataGrid ColumnHeaderStyle new aspx page with xaml path create silverlight enabled page aspx silverligh 2 Beta2 navigate to other page silverlight 2 beta 2 Silverlighlight 2 Beta 2 DataGridTextColumn Click Event Silverlight 2.0 B2 silverlight 2.0 silverlight beta2 IE Silverlight 2 Beta2 Silvertlight 2 Beta 2 Silverlighight 2 Beta 2 Silverlight 2 Beta2 Cairngorm Silverlight2 Beta2 Widets Silverligh 2 Beta 2 setInterval Animation Silverlight 2 Release Candidate Silverlight 2 RC0 silverlight2 xml xsd panel Silveright 2 custom contros silverlight bug 2 asp.net Silverlight 2 Release Silveright 2 XML silveright 2 popup Silverlight2 silveright 2 effect silvelight silvelight 2 silveright 2 page effect silveright 2 process Silvelight 2 DependencyProperty Collection Fails Silvelight 2.0 Silverligh 2 WCF new browser window Silverightlight 2 Silveright 2.0 silvelight 2 IE7 "Silvelight 2" binding xaml

  • EliteMike

    EliteMike

    Member

    65 Points

    25 Posts

    Re: Printing silverlight control in silverlight 2.0

    May 20, 2009 04:50 PM | LINK

    Silverlight has no printing support.  What you can do is set up a webservice that uses wcf to create an image from the xaml.  That's the only way I know to do it right now.

  • kondamca

    kondamca

    Member

    27 Points

    99 Posts

    Re: Re: Printing silverlight control in silverlight 2.0

    May 21, 2009 04:36 AM | LINK

    Hi EliteMike,

    Can you explain me indetail how to do printing with web service call. Please provide me some example.

    Thanks,

    Konda.

  • EliteMike

    EliteMike

    Member

    65 Points

    25 Posts

    Re: Re: Printing silverlight control in silverlight 2.0

    May 21, 2009 02:22 PM | LINK

    I included code below that I use in my own project, I hope  you can get an understanding from this.
    This is all done in a webservice so you need to configre one first
    I'm almost positive this is the basic template of what you need to do this, I only removed code that 
    was specifc to my project.  
    There may be a way to pass in your xaml from silverlight, but I actually just rebuild mine in wpf.  I pass in some parameters to 
    the GenerateImage sub and build based on my parameters.
     
    1    Public WithEvents MyImage As Canvas ' Root canvas of the image generated
    
    Private Delegate Sub CanvasDelegate(ByVal o As Canvas)
    
    Private retImageStream As System.IO.Stream ' Stream used to return the image to the client.
    
    Dim Width As Double = 10
    
    Dim Height As Double = 8
    
    Const SCREEN_DPI As Double = 96.0 ' Screen DPI
    
    Const TARGET_DPI As Double = 300.0 ' Print Quality DPI
    
    
    
    Public Sub GenerateImage()
            Dim thread As System.Threading.Thread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf CreateImage))
            thread.SetApartmentState(System.Threading.ApartmentState.STA)
            thread.IsBackground = True
            thread.Start()  ' Start the thread
            thread.Join()   ' Joining with the thread causes execution of the remaining lines of this method to wait for the thread to complete.
    
        End Sub
    
      Public Sub CreateImage()
                    MyImage.Children.Add(YourElements)
    
    
                    '' These lines ensure that our new visual tree elements will be processed and end up on our final image.
                    MyImage.Measure(New System.Windows.Size(Width, Height))
                    MyImage.Arrange(New System.Windows.Rect(0, 0, Width, Height))
                    MyImage.UpdateLayout()
    
            MyImage.Clip = New RectangleGeometry(New Rect(0, 0, 800, 600))
    
    
            Dim cnv As New Canvas
    
            ' Invoke the delegate to render the WPF visual tree to an image.
            Dim slt As CanvasDelegate
            slt = New CanvasDelegate(AddressOf Capture)
            MyImage.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, slt, MyImage)
      End Sub
    
    
        Public Sub Capture(ByVal MyImage As Canvas)
            ' Normally we would obtain a user's configured DPI setting to account for the possibilty of a high DPI setting. 
            ' However, this code is running server side so the client's DPI is not obtainable.
            Const SCREEN_DPI As Double = 96.0   ' Screen DPI
            Const TARGET_DPI As Double = 300.0  ' Print Quality DPI
    
            ' Determine the constraining scale to maintain the aspect ratio and the bounds of the image size
            Dim scale As Double = Math.Min(Width * SCREEN_DPI / MyImage.Width, Height * SCREEN_DPI / MyImage.Height)
    
            ' Setup the bounds of the image
            Dim bounds As Rect = New Rect(0, 0, MyImage.Width * scale, MyImage.Height * scale)
            MyImage.Measure(New Size(MyImage.Width * scale, MyImage.Height * scale))
            MyImage.Arrange(bounds)
    
            Dim pixHeight As Integer = CDbl(MyImage.Height * scale / SCREEN_DPI * TARGET_DPI)
            Dim pixWidth As Integer = CDbl(MyImage.Width * scale / SCREEN_DPI * TARGET_DPI)
    
            ' Create the target bitmap
            Dim rtb As RenderTargetBitmap = New RenderTargetBitmap(pixWidth, pixHeight, TARGET_DPI, TARGET_DPI, PixelFormats.Pbgra32)
    
            ' Render the image to the target bitmap
            Dim dv As DrawingVisual = New DrawingVisual()
            Using ctx As DrawingContext = dv.RenderOpen()
                Dim vb As New VisualBrush(MyImage)
                ctx.DrawRectangle(vb, Nothing, New Rect(New System.Windows.Point(), bounds.Size))
            End Using
    
            ' Render the visual to the bitmap.
            rtb.Render(dv)
    
            'Create PNG
            Dim image As New PngBitmapEncoder
            image.Frames.Add(BitmapFrame.Create(rtb))
            Dim pathLoc As String = 'Your Path'
            Dim stm As Stream = File.Create(System.IO.Path.Combine(pathLoc, "FileName.png"))
            Clipboard.SetImage(rtb)
            Try
                image.Save(stm)
            Finally
                stm.Dispose()
            End Try
    
        End Sub