开发者

WPF: How to create a BitmapImage... from another BitmapImage?

I have a method where I accept as a parameter a BitmapImage that is small say 25x25

public BitmapImage MakeLargerAndAddBorder(BitmapImage smallImage)
{
   ...
}

I'd like this method to return a bigger image say 50x50, that programmatically puts the smaller image in the middle (doesn't scale or change this smallerImage mind you), gives it a background color, and slaps a white border on it. I'm wondering if this is even possible? It's a long story but I don't have the URL to the image, just an existing BitmapImage object... I'm wondering开发者_如何学编程 if I can somehow programmatically convert this to a larger image? I've been googling for this but I can't really find a way to programmatically create a BitmapImage. (esp from an existing one)


You want WriteableBitmap. :) Just create a WriteableBitmap with the same parameters as your original (with the modified size). Then lock the bitmap and use its BackBuffer property to access the underlying pixels. You should be able to use the CopyPixels() method to copy the original image into your new one.


Here is an example of how to do this in XAML with a bit of code-behind.

<Border Width="50" Height="50" BorderThickness="2" BorderBrush="Black">
  <Image x:Name="myImage" Height="25" 
                          Width="25"/>
</Border>

In the code behind, if you can not provide a Source URL (which you have said you can't), you can set the source property of the Image control.

myImage.Source = bitmap; // your bitmap object.

If you do not set the size on your Image and Border in the XAML, they should expand to whatever size is required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜