Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

The image is large and loaded it in run-time, actually I want resize selfsame to a small image under WPF facilities without GDI+.
In other words, I have an ImageBrush and set an image large in its ImageSource in run-time, so I want resizing it to small.

Any help pls...

Thanks in advance.
Posted
Updated 1-Nov-11 10:46am
v5

1 solution

An image is still an image - so things like
Build an ASP.NET Thumbnail Image Generator[^] provides the code required to create the thumbnail.

And then do something like WPF In-Memory Image Display[^]

You can also use RenderTargetBitmap[^]
public static BitmapSource CreateBitmapSourceFromVisual(double width, double height, Visual visual)
{
    if (visual == null)
    {
        return null;
    }
    RenderTargetBitmap bmp = new RenderTargetBitmap((Int32)Math.Ceiling(width),(Int32)Math.Ceiling(height),300,300,PixelFormats.Pbgra32);
 
    bmp.Render(visual);
    return bmp;
}




Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Amir Mahfoozi 1-Nov-11 14:47pm    
+5 for providing all required resources
Espen Harlinn 1-Nov-11 14:51pm    
Thank you, Amir :)
hzawary 1-Nov-11 14:56pm    
Thanks Espen Harlinn for your consideration, actually I want change a large image to small with WPF facilities.
hzawary 2-Nov-11 7:16am    
Thanks again Espen, I tried your beautiful code. I want a program like to this

myImageBrush.ImageSource = CreateBitmapSourceFromVisual(100, 100, myImageBrush);

But it has an error. May you more help about this, please?
Espen Harlinn 2-Nov-11 8:57am    
System.Windows.Media.Brush is not derived from System.Windows.Media.Visual.
System.Windows.Controls.Image is derived from System.Windows.Media.Visual so if you use an instance of System.Windows.Controls.Image and then call CreateBitmapSourceFromVisual, passing the image and not a brush, you should have something that works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900