c# - Dynamically crop a BitmapImage object -


i have bitmapimage object contains image of 600 x 400 dimensions. c# code behind, need create 2 new bitmapimage objects, obja , objb of dimensions 600 x 200 each such obja contains upper half cropped image , objb contains lower half cropped image of original image.

bitmapsource tophalf = new croppedbitmap(sourcebitmap, toprect); bitmapsource bottomhalf = new croppedbitmap(sourcebitmap, bottomrect); 

the result not bitmapimage, it's still valid imagesource, should ok if want display it.


edit: there way it, it's pretty ugly... need create image control original image, , use writeablebitmap.render method render it.

image imagecontrol = new image(); imagecontrol.source = originalimage;  // required because image control not part of visual tree (see doc) size size = new size(originalimage.pixelwidth, originalimage.pixelheight); imagecontrol.measure(size); rect rect = new rect(new point(0, 0), size); imagecontrol.arrange(ref rect);  writeablebitmap tophalf = new writeablebitmap(originalimage.pixelwidth, originalimage.pixelheight / 2); writeablebitmap bottomhalf = new writeablebitmap(originalimage.pixelwidth, originalimage.pixelheight / 2);  transform transform = new translatetransform(); tophalf.render(originalimage, transform); transform.y = originalimage.pixelheight / 2; bottomhalf.render(originalimage, transform); 

disclaimer: code untested ;)


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -