how to get BitmapImage in codebehind from the image tag in xaml in wpf/silverlight

后端 未结 2 1950
难免孤独
难免孤独 2021-01-24 07:36

i dont have a problem with binding a bitmapimage to image tag in codebehind for eg.

BitmapImage image = new BitmapImage();
imagetaginxaml.Source = image; // this         


        
2条回答
  •  春和景丽
    2021-01-24 08:18

    How about using WriteableBitmap to make a copy of the image, and then using a MemoryStream to copy the original image into a copy?

    // Create a WriteableBitmap from the Image control
    WriteableBitmap bmp = new WriteableBitmap(imagetaginxaml, null);
    
    // Load the contents of a MemoryStream from the WritableBitmap
    MemoryStream m = new MemoryStream();
    bmp.SaveJpeg(m, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
    
    // Read from the stream into a new BitmapImage object
    m.Position = 0;
    BitmapImage image = new BitmapImage();
    image.SetSource(m);
    
    // do something with the new BitmapImage object
    // (for example, load another image control)
    anotherimagetaginxaml.Source = image;
    

提交回复
热议问题