Interaction between winform control and WPF control

Source: Internet
Author: User

This problem can also be understood:How to use controls in winform (such as picturebox) in WPF/XAML )?
First, let's look at the XAML code:(Note the bold section below)
<Window X: class = "windowsapplication1.window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "Interaction Between winform control and WPF control" Height = "400" width = "600"
Xmlns: WF = "CLR-namespace: system. Windows. forms; Assembly = system. Windows. Forms"
>
<Stackpanel>
<Grid>
<Grid. columndefinitions>
<Columndefinition/>
<Columndefinition/>
</Grid. columndefinitions>
<Image Source = "girl.jpg" maxheight = "300" grid. Column = "0" name = "wpfimage"/>
<Windowsformshost grid. Column = "1" maxheight = "300" name = "picturehost">
<WF: picturebox/>
</Windowsformshost>
</GRID>
</Stackpanel>
</WINDOW>
C # code:
System. Windows. Forms. picturebox _ picturebox = NULL;
Void windowreceivloaded (Object sender, routedeventargs E)
{
_ Picturebox = picturehost. Child as system. Windows. Forms. picturebox;
_ Picturebox. Image = getbitmap (wpfimage );
}

# Region image Functions
// The following code implements interaction between the two WPF and GDI +
Public void converttograyscale (system. Drawing. bitmap source, int sliderval)
{
System. Drawing. Bitmap Bm = new system. Drawing. Bitmap (source. Width, source. Height );
// The following code can also use Insecure code to improve efficiency
For (INT y = 0; y <BM. height; y ++)
{
For (INT x = 0; x <BM. width; X ++)
{
System. Drawing. Color c = source. getpixel (x, y );
Int Luma = (INT) (C. R * (double) sliderval/(double) 10) * 0.3 + C. g * (double) sliderval/(double) 10) * 0.59 + C. B * (double) sliderval/(double) 10) * 0.11 );
BM. setpixel (X, Y, system. Drawing. color. fromargb (Luma, Luma, Luma ));
}
}
_ Picturebox. Image = bm;
}

System. Drawing. Bitmap adjustbrightnessmatrix (system. Drawing. Bitmap IMG, int value)
{
If (value = 0) // No change, so just return
Return IMG;

Float sb = (float) value/255f;
Float [] [] colormatrixelements =
{
New float [] {1, 0, 0, 0, 0 },
New float [] {0, 1, 0, 0, 0 },
New float [] {0, 0, 1, 0, 0 },
New float [] {0, 0, 0, 1, 0 },
New float [] {Sb, 1, 1}
};

System. Drawing. imaging. colormatrix CM = new system. Drawing. imaging. colormatrix (colormatrixelements );
System. Drawing. imaging. imageattributes imgattr = new system. Drawing. imaging. imageattributes ();
System. Drawing. Rectangle rc = new system. Drawing. rectangle (0, 0, IMG. Width, IMG. Height );
System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (IMG );
G. interpolationmode = system. Drawing. drawing2d. interpolationmode. highqualitybicubic;
Imgattr. setcolormatrix (CM );
G. drawimage (IMG, RC, 0, 0, IMG. Width, IMG. Height, system. Drawing. graphicsunit. pixel, imgattr );

Imgattr. Dispose ();
G. Dispose ();
Return IMG;
}
# Endregion image Functions

# Region image-bitmap InterOP helpers
Private void convertbitmaptobitmapsource (system. Drawing. Bitmap bitmap)
{
Using (Bitmap)
{
System. Windows. Media. imaging. bitmapsource = system. Windows. InterOP. imaging. createbitmapsourcefromhbitmap (
Bitmap. gethbitmap (),
Intptr. Zero,
Int32rect. empty,
System. Windows. Media. imaging. bitmapsizeoptions. fromemptyoptions ());

Wpfimage. Source = bitmapsource;
}
}

Private system. Drawing. Bitmap getbitmap (image)
{
System. Windows. Forms. picturebox picture = _ picturebox;
// Stream STM = file. Open ("waterfall.jpg", filemode. Open, fileaccess. Read ))
/// Since we're not specifying a system. Windows. Media. imaging. bitmapcacheoption, the pixel format
/// Will be system. Windows. Media. pixelformats. pbgra32.
// System. Windows. Media. imaging. bitmapsource = system. Windows. Media. imaging. bitmapframe. Create (
// STM,
// System. Windows. Media. imaging. bitmapcreateoptions. None,
// System. Windows. Media. imaging. bitmapcacheoption. onload );

System. Windows. Media. imaging. bitmapsource = wpfimage. source as bitmapsource;

// Scale the image so that it will display similarly to the WPF image.
Double newwidthratio = picture. width/(double) bitmapsource. pixelwidth;
Double newheightratio = (picture. Width * bitmapsource. pixelheight)/(double) bitmapsource. pixelwidth)/(double) bitmapsource. pixelheight;

System. Windows. Media. imaging. bitmapsource transformedbitmapsource = new system. Windows. Media. imaging. transformedbitmap (
Bitmapsource,
New system. Windows. Media. scaletransform (newwidthratio, newheightratio ));

Int width = transformedbitmapsource. pixelwidth;
Int Height = transformedbitmapsource. pixelheight;
Int stride = width * (transformedbitmapsource. format. bitsperpixel + 7)/8 );

Byte [] bits = new byte [height * stride];

Transformedbitmapsource. copypixels (bits, stride, 0 );

Unsafe
{
Fixed (byte * pbits = bits)
{
Intptr PTR = new intptr (pbits );

System. Drawing. Bitmap bitmap = new system. Drawing. Bitmap (
Width,
Height,
Stride,
System. Drawing. imaging. pixelformat. format32bpppargb,
PTR );

Return bitmap;
}
}
}
# Endregion image-bitmap InterOP helpers
Imagine that through mutual conversion and mutual calling, you can easily implement some functions (complementary functions ).

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.