Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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;
}

Because I want resizing the myImageBrush like this program:
C#
myImageBrush.ImageSource = CreateBitmapSourceFromVisual(100, 100, myImageBrush);


Any help pls...

Thanks in advance.
Posted
Updated 2-Nov-11 4:16am
v5
Comments
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.

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

Example:

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.decodepixelheight.aspx[^]
 
Share this answer
 
Comments
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.BeginInit();
bmpImg.DecodePixelWidth = bmpImg.DecodePixelHeight= 50;
bmpImg.UriSource = new Uri(srcImg);
bmpImg.EndInit();

BkImageTScreen.ImageSource = bmpImg.Clone() as BitmapImage;
bmpImg = null;
GC.Collect();
}
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);
 g.Dispose();
 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.
 
Share this answer
 
Comments
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 = "";
bmp.Save(dest);
}

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