Silverlight - How to load and clip an image in code

by rahul 1/31/2011 1:21:08 PM

Let's say you have an image like the following…

image

Fig 1

And you want to clip it using a hexagon [see Fig 2]. Or may be a rectangle with rounded edges. Clipping an image using a Geometry makes it really easy for you. You can use Expression Blend and easily do the clipping. The problem arises if you have to do the same thing in code.

image

Fig 2

In this post, you will see how easily it can be done in code. The code is annotated with comments, and I hope you will find it useful.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ImageClip
{
    public partial class MainPage : UserControl
    {
        public int Image_Top_X { get; set; }
        public int Image_Top_Y { get; set; }
        public int Image_Bottom_X { get; set; }
        public int Image_Bottom_Y { get; set; }
        public int Image_Radius_X { get; set; }
        public int Image_Radius_Y { get; set; }

        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Initialize the size of the images
            Image_Top_X = 0;
            Image_Top_Y = 0;
            Image_Bottom_X = 200;
            Image_Bottom_Y = 200;
            Image_Radius_X = 10;
            Image_Radius_Y = 10;

            //Load the Peacock image at runtime
            Uri uri = new Uri("Images/peacock.png", UriKind.Relative);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);

            //Set the value for Image1
            image1.SetValue(Image.SourceProperty, img);
            image1.Width = Image_Bottom_X;
            image1.Height = Image_Bottom_Y;
            image1.Stretch = Stretch.Fill;

            //Set Hexagon geometry as its clipping path
            PathGeometry myPathGeometry = HexagonGeometry();
            image1.Clip = myPathGeometry;

            //Set the value for Image1
            image2.SetValue(Image.SourceProperty, img);
            image2.Width = Image_Bottom_X;
            image2.Height = Image_Bottom_Y;
            image2.Stretch = Stretch.Fill;
            
            //Make the corners rounded
            RectangleGeometry myRectangleGeometry = RoundedRectangleGeometry();
            image2.Clip = myRectangleGeometry;
        }

        private PathGeometry HexagonGeometry()
        {
            PathFigure pf = new PathFigure();
            pf.IsClosed = true;

            //Declare the starting point for the path
            pf.StartPoint = new Point((Image_Bottom_X - Image_Top_X) * .25, Image_Top_Y);

            //Add Line segments
            LineSegment l1 = new LineSegment();
            l1.Point = new Point(Image_Bottom_X * .75, Image_Top_Y);
            LineSegment l2 = new LineSegment();
            l2.Point = new Point(Image_Bottom_X, Image_Bottom_Y * .5);
            LineSegment l3 = new LineSegment();
            l3.Point = new Point(Image_Bottom_X * .75, Image_Bottom_Y);
            LineSegment l4 = new LineSegment();
            l4.Point = new Point((Image_Bottom_X - Image_Top_X) * .25, Image_Bottom_Y);
            LineSegment l5 = new LineSegment();
            l5.Point = new Point(Image_Top_X, Image_Bottom_Y / 2);

            //Add the Line Segments to the Path Segment
            PathSegmentCollection CustomPathSegmentCollection = new PathSegmentCollection();
            CustomPathSegmentCollection.Add(l1);
            CustomPathSegmentCollection.Add(l2);
            CustomPathSegmentCollection.Add(l3);
            CustomPathSegmentCollection.Add(l4);
            CustomPathSegmentCollection.Add(l5);

            //Specify the PathFigure segment
            pf.Segments = CustomPathSegmentCollection;

            PathFigureCollection CustomPathFigureCollection = new PathFigureCollection();
            CustomPathFigureCollection.Add(pf);

            PathGeometry myPathGeometry = new PathGeometry();
            myPathGeometry.Figures = CustomPathFigureCollection;
            return myPathGeometry;
        }

        private RectangleGeometry RoundedRectangleGeometry()
        {
            //This one is fairly simple when compared to Path geometry. 
            //All that is required is a rectangle geometry
            RectangleGeometry r = new RectangleGeometry();
            r.RadiusX = Image_Radius_X;
            r.RadiusY = Image_Radius_Y;
            r.Rect = (new Rect(Image_Top_X, Image_Top_Y, Image_Bottom_X, Image_Bottom_Y));
            return r;
        }
    }
}

Until next time, Wave
Rahul

 



Quote of the day:
When ideas fail, words come in very handy. - Johann Wolfgang von Goethe


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0