Click here to Skip to main content
11,929,166 members (53,069 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XAML WPF
Hi everybody,

I have an ImageBrush and set an image large in its ImageSource in run-time, so I want resizing it to small image.
In other words, The image is large and loaded it in run-time, actually I want resize (in-place) selfsame to a small image under WPF facilities, don't use 'System.Drawing' namespaces.

I also used this method, but it has an error for this my problem:
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);
    return bmp;
Because I want resizing the myImageBrush like this program:
myImageBrush.ImageSource = CreateBitmapSourceFromVisual(100, 100, myImageBrush);

Any help pls...

Thanks in advance.
Posted 2-Nov-11 0:38am
Edited 2-Nov-11 5:16am
Wayne Gaylard 2-Nov-11 6:23am
What format is the image in. i.e is it png, jpg or something else ?
hzawary 2-Nov-11 8:04am
It should respond to me in any format.
BobJanova 2-Nov-11 8:11am
'Without GDI+' doesn't really make sense since the .Net Framework uses GDI+ for all its rendering. I think you mean without P/Invoking gdiplus.dll?
hzawary 2-Nov-11 8:29am
With 'using System.Windows.Media' namespaces or like it, and without 'System.Drawing' namespaces or such it!
BobJanova 2-Nov-11 10:18am
System.Drawing is included in WPF (as it's in the Framework). What is the reason you don't want to use it?

I've not used S.W.Media so I can't help with that.
hzawary 2-Nov-11 10:28am
Because doesn't match 'System.Drawing.Image' to 'System.Windows.Media.ImageBrush'!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should be able to create the smaller image at run time too. You'd most likely need to set DecodePixelWidth and DecodePixelHeight accordingly.

hzawary 2-Nov-11 12:10pm
My 5, You are rescue angel!

Just a few lines, get of all time!

string srcImg = GetCurrentWallpaperPath();
if (srcImg != "")
BitmapImage bmpImg =new BitmapImage();
bmpImg.DecodePixelWidth = bmpImg.DecodePixelHeight= 50;
bmpImg.UriSource = new Uri(srcImg);

BkImageTScreen.ImageSource = bmpImg.Clone() as BitmapImage;
bmpImg = null;
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use Graphics.DrawImage[^]:
public Image Resize(Image i, int w, int h){
 Bitmap r = new Bitmap(w, h);
 Graphics g = Graphics.FromImage(r);
 g.DrawImage(i, 0, 0, w, h);
 return r;

Note that this produces a raster resize, which is probably what you want for a thumbnail. If you want to be clever you'd have to construct a new image of the same type as the one passed in to you. But since you're using a bitmap in your example, it seems reasonable to do that here.
hzawary 2-Nov-11 8:19am
Thanks BobJanova, but I tried this method, I want acting with WPF facilities.

using (System.Drawing.Image image = System.Drawing.Image.FromFile(src))
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(110, 74))
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp))
int srcWidth = image.Width;
int srcHeight = image.Height;
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, 110, 74);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, System.Drawing.GraphicsUnit.Pixel);
string dest = "";

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 2 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100