‘ 貌似WPF对GDI+不提供支持,要达到剪裁图像的方法,可以使用image.clip,
’不过clip只是对图片的一个遮挡拦截效果,并不改变本身的图片资源。
‘下面的代码提供了剪裁图片资源的方法。
Dim path = "C:\Users\Administrator\Desktop\image\image\Images\tori_gaku_02.png"
Dim bitmap As BitmapSource = New BitmapImage(New Uri(path, UriKind.Absolute))
'获得位图宽度,高度
Dim width = bitmap.PixelWidth
Dim height = bitmap.PixelHeight
Dim bytePerPixel = bitmap.Format.BitsPerPixel / 8
Dim stride = width * bytePerPixel
'储存位图像素信息
Dim pixls(stride * height + 1) As Byte
Dim dpix = bitmap.DpiX
Dim dpiy = bitmap.DpiY
Dim format = bitmap.Format 'PixelFormats.Bgr32 '
Dim palette = bitmap.Palette ' Nothing
'设置剪切矩形
Dim cut As New Int32Rect(0, 0, width * 0.25, height * 0.25)
Dim cut2 As New Int32Rect(width * 0.25, height * 0.25, width * 0.25, height * 0.25)
'复制指定区域像素信息
bitmap.CopyPixels(cut2, pixls, stride, 0) '用法2:bitmap.CopyPixels(pixls, stride, 0)
'根据复制的像素信息,新建一个位图
Dim image As BitmapSource = BitmapSource.Create(width * 0.25, height * 0.25, dpix, dpiy, format, palette, pixls, stride)
'新建一个image控件,显示剪裁的位图
Dim image2 As New Image
image2.Stretch = Stretch.Fill
image2.Width = width * 0.25
image2.Height = height * 0.25
image2.Source = image
grid1.Children.Add(image2)