Question

I want to add a drop shadow to an image file. What's the best way to do that? I thought about creating a WPF Image control and adding a bitmap effect.. But how can I save the result to a file?

Thanks, Eric

Was it helpful?

Solution

You can use RenderTargetBitmap and an Encoder to do this. Encoder can be Png,Jpeg etc.. Below code imgControl represents your Image control. But since it is a bitmap effect you might need to put this Image inside a grid and give proper margin equivalent to the dropshadow and then instead of imgControl use the grid in the below code.

double Height = imgControl.ActualHeight;
double Width = imgControl.ActualWidth;

RenderTargetBitmap bmp = new RenderTargetBitmap((int)Width, (int)Height, 
                                                96, 96, PixelFormats.Pbgra32);
bmp.Render(imgControl);

BitmapEncoder encoder = new JpegBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(bmp));

using (Stream stream = File.Create("Yourfile.jpeg"))
{
    encoder.Save(stream);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top