locked
Crop Image in Silverlight5 RRS feed

  • Question

  • Hi Guys,

    On printing time I want to crop Image in Silverlight5.I tried with this code.Actually I want that Image need to corp from bellow,I tried with this.

    For WPF this code is working fine..

      CroppedBitmap cb = new CroppedBitmap(image,
                            new Int32Rect(0,0,
                                Math.Min((int)image.Width, image.PixelWidth),
                                Math.Min((int)image.Height-80, image.PixelHeight)));

    Here Image =  var image = GenerateBitmapSource(rootElement);

    But in Silverlight we don't have CroppedBitmap .So I tried with bellow code

    WriteableBitmap cb = new WriteableBitmap(image.PixelWidth, image.PixelHeight-80);

                    var imageSnapshot = new Image()
                    {
                        Source = cb,
                        Stretch = stretchToFit ? Stretch.Uniform : Stretch.None,
                        VerticalAlignment = VerticalAlignment.Center,
                        HorizontalAlignment = HorizontalAlignment.Center,
                    };

    rootElement is FrameworkElement..

    But it is not working Please help....

    Regards,

    Rahman



    • Edited by Rahman1036 Friday, December 20, 2013 4:17 PM
    Friday, December 20, 2013 10:36 AM

All replies

  • Hello,

    This link provided a solution about how to Crop instead of Clip an Image in Silverlight

    The solution is to use a smaller Writeable Bitmap to copy the image into. In your code create a Writeable Bitmap object the size of the final cropped area (in this case 50×50).  Then use the Render method to copy the Image element on to the Writeable Bitmap. When calling the Render method you will have to pass in a translate transform to move the area of the image you wish to crop in to the top left corner. Finally set the source of your image to the new Writeable Bitmap.

    //Create a bitmap of the cropped size
    WriteableBitmap wb = new WriteableBitmap(50, 50 );
     
    //Create a transform to move the Image to the top left
    TranslateTransform t = new TranslateTransform();
    t.X = -70;
    t.Y = -70;
     
    //Draw to the Writeable Bitmap
    wb.Render(sampleImage,t);
    wb.Invalidate();
                            
     //Finally set the Image back
     sampleImage.Source = wb;
    

    Hope this helps, thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 23, 2013 6:03 AM
  • Hi,

    Bellow there is my full code sample. Actually runtime I am generating image.

    public class PrintAction : TargetedTriggerAction<FrameworkElement>

        {

            protected override void Invoke(object parameter)

            {

                FrameworkElement rootElement = base.TargetObject as FrameworkElement;

                if (rootElement != null)

                {

    #if SILVERLIGHT

                    var stretchToFit = false;

                    PrintDocument doc = new PrintDocument();

                    EventHandler<EndPrintEventArgs> endPrintHandler = null;

                    endPrintHandler = (sender, e) =>

                    {

                        doc.EndPrint -= endPrintHandler;

                    };

                    doc.EndPrint += endPrintHandler;

                    EventHandler<PrintPageEventArgs> printPageHandler = null;

                    printPageHandler = (sender, e) =>

                    {

                        doc.PrintPage -= printPageHandler;

                        e.PageVisual = GeneratePrintPage(rootElement, stretchToFit, e.PrintableArea);

                    };

                    doc.PrintPage += printPageHandler;

                    doc.Print(this.DocumentName);

    #else

                    var stretchToFit = false;

                    var printDialog = new PrintDialog();

                    if (printDialog.ShowDialog().GetValueOrDefault())

                    {

                        var capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);

                        if (capabilities.PageImageableArea != null)

                        {

                            var printAreaSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

                            var page = GeneratePrintPage(rootElement, stretchToFit, printAreaSize);

                            page.Measure(printAreaSize);

                            page.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), page.DesiredSize));

                            printDialog.PrintVisual(page, DocumentName);

                        }

                    }

    #endif

                }

            }

            protected virtual UIElement GeneratePrintPage(FrameworkElement rootElement, bool stretchToFit, Size printArea)

            {

                if (!printArea.IsEmpty)

                {

                    var image = GenerateBitmapSource(rootElement);

                 

                    var grid = new Grid()

                    {

                        Width = printArea.Width,

                        Height = printArea.Height,

                        Background = new SolidColorBrush(Colors.White)

                    };

                    RowDefinition rowDefinition0 = new RowDefinition();

                    RowDefinition rowDefinition1 = new RowDefinition();

                    RowDefinition rowDefinition2 = new RowDefinition();

                    rowDefinition0.Height = new GridLength(50);

                    rowDefinition1.Height = new GridLength(50);

                    int secondRowHeight = Convert.ToInt32(rootElement.ActualHeight) + 50;

                    if (secondRowHeight <= printArea.Height)

                    {

                        rowDefinition2.Height = new GridLength(secondRowHeight);

                    }

                    grid.RowDefinitions.Add(rowDefinition0);

                    grid.RowDefinitions.Add(rowDefinition1);

                    grid.RowDefinitions.Add(rowDefinition2);

                    grid.Children.Add(documentName);

                    Grid.SetRow(documentName, 1);

    #if !SILVERLIGHT

                    CroppedBitmap cb = new CroppedBitmap(image,

                            new Int32Rect(0,0,

                                Math.Min((int)image.Width, image.PixelWidth),

                                Math.Min((int)image.Height-80, image.PixelHeight)));

    #else

                   

                    WriteableBitmap cb = new WriteableBitmap(260,285);

                    //Create a transform to move the Image to the top left

                    TranslateTransform t = new TranslateTransform();

                    t.X = -70;

                    t.Y = -70;

                    cb.Render(image , t);----------Here is the problem (image is not Frameworkelement and if I passed rootElement as UIElement is not printing anything)

                    cb.Invalidate();

                  

    #endif

                    var imageSnapshot = new Image()

                    {

                        Source = cb,

                        Stretch = stretchToFit ? Stretch.Uniform : Stretch.None,

                        VerticalAlignment = VerticalAlignment.Center,

                        HorizontalAlignment = HorizontalAlignment.Center,

                    };

                    grid.Children.Add(imageSnapshot);

                    Grid.SetRow(imageSnapshot, 2);

                    return grid;

                }

                return null;

            }

            protected virtual BitmapSource GenerateBitmapSource(FrameworkElement rootElement)

            {

                FrameworkElement tempElement = rootElement;

                if (rootElement.ActualWidth > rootElement.ActualHeight)

                {

                    tempElement = rootElement;

    #if !SILVERLIGHT

                    tempElement.LayoutTransform = new RotateTransform(0, 0, 0);

    #else

                    //tempElement.System.LayoutTransform = new RotateTransform();

    #endif

                }

    #if SILVERLIGHT

                var imageSource = new WriteableBitmap(rootElement, null);

    #else

                var renderTargetBitmap = new RenderTargetBitmap(Convert.ToInt32(tempElement.ActualWidth), Convert.ToInt32(tempElement.ActualHeight), 96, 96, PixelFormats.Default);

                renderTargetBitmap.Render(tempElement);

                var imageSource = BitmapFactory.New((int)renderTargetBitmap.Width, (int)renderTargetBitmap.Height);

                renderTargetBitmap.CopyPixels(new Int32Rect(0, 0, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight),

                    imageSource.BackBuffer,

                    imageSource.BackBufferStride * imageSource.PixelHeight,

                    imageSource.BackBufferStride);

    #endif

                var themeManager = ClassFactory.Container.GetExportedValue<IThemeManager>();

                if (themeManager != null &&

                    (themeManager.CurrentTheme == themeManager.BlackTheme || themeManager.CurrentTheme == themeManager.BlueTheme))

                {

                    imageSource.ForEach((x, y, c) =>

                    {

                        byte gray = (byte)Math.Min(255, c.R * 0.299 + c.G * 0.587 + c.B * 0.114);

                        return Color.FromArgb((byte)Math.Min(255, 1.4 * c.A), gray, gray, gray);

                    });

                    return imageSource.Invert();

                }

                return imageSource;

            }

        }

    Please Help...!!!

    Thanks in advanced....

     

    Rahman


    • Edited by Rahman1036 Tuesday, December 24, 2013 10:40 AM
    Tuesday, December 24, 2013 10:36 AM