Silverlight: How to make a hole in the image?

Source: Internet
Author: User

1. Do not writeCodeMethod: Use Blend

Figure-based speech:

This is the image to be processed.

Draw a rectangle on win7 and use a pen to draw a closed path.

Combine the rectangle and path to form a complex path

Select the merged complex path and win7 image at the same time, and then generate a cut path

In this way, we get an irregular image contour (of course, the demonstration here removes the irregular part, and the reverse is digging holes)

 

Ii. Use code to dig holes

Principle: Use writeablebitmap to copy the original image, hide the source image, and set the pixel transparency of the specified area to 0.

Code:

XAML Section

< Usercontrol
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D"
X: Class = "Mark. Index"
D: designwidth = "400" D: designheight = "250" Width = "400" Height = "250"   >

< Grid X: Name = "Layoutroot" >
< Image X: Name = "Win7" Source = "Img/win7.jpg" Width = "400" Height = "250" D: ishidden = "True" />
< Image X: Name = "Imgmask" D: ishidden = "True"   > </ Image >
</ Grid >
</ Usercontrol >

 

Backend CS

Using System;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. Media. imaging;

Namespace Mark
{
Public   Partial   Class Index: usercontrol
{
Public Index ()
{
// Required to initialize Variables
Initializecomponent ();

This. Loaded+ = NewRoutedeventhandler (index_loaded );
}

Void Index_loaded ( Object Sender, routedeventargs E)
{
Writeablebitmap WB =   New Writeablebitmap (win7, Null );
This . Imgmask. Source = WB;
Win7.visibility = Visibility. collapsed;
Genmask (WB );
}

void genmask (writeablebitmap WB)
{< br> int _ width = ( int ) win7.width;
int _ height = ( int ) win7.height;

# Region Empty the area with a margin of less than 50 PX.
Int _ Padding =   50 ;

For ( Int Row = _ Padding +   1 ; Row < _ Height - _ Padding; row ++ )
{
For ( Int I = _ Width * Row + _ Padding; I < _ Width * (Row + 1) - _ Padding; I ++ )
{
WB. pixels [I] = Bitconverter. toint32 ( New   Byte [] { 0 , 0 , 0 , 0 }, 0 ); // Note: The meaning of the byte array is {B, G, R, a} in sequence, that is, {blue, green, red, transparency}
}
}
# Endregion

}

}
}

Effect:

This can also be used to play tricks (add white noise in the specified area ):


Empty the mouse to erase the effect:


Finally, I would like to add a bit of Bitmap pixel knowledge:

In bitmap, pixel is stored as an int32 integer array. The length of the array is equal to the width of the image * the height of the image. After each element of the array is split into byte [], there are four components, that is, R, G, B, A-red, green, blue, and transparency. If we want to reverse the color of the image (for example, change the red car to a green car), we only need to exchange the corresponding components.

reprinted please indicate from the bodhi tree Yang Guo http://www.cnblogs.com/yjmyzz/archive/2010/01/20/1652391.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.