Click here to Skip to main content
Rate this: bad
good
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);
 
            bmp.Render(visual);
            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
hzawary1.5K
Edited 2-Nov-11 5:16am
v5
Comments
Wayne Gaylard at 2-Nov-11 6:23am
   
What format is the image in. i.e is it png, jpg or something else ?
hzawary at 2-Nov-11 8:04am
   
It should respond to me in any format.
BobJanova at 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 at 2-Nov-11 8:29am
   
With 'using System.Windows.Media' namespaces or like it, and without 'System.Drawing' namespaces or such it!
BobJanova at 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 at 2-Nov-11 10:28am
   
Because doesn't match 'System.Drawing.Image' to 'System.Windows.Media.ImageBrush'!
Rate this: bad
good
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.
 
Example:
 
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.decodepixelheight.aspx[^]
  Permalink  
Comments
hzawary at 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();
}
Rate this: bad
good
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);
 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.
  Permalink  
Comments
hzawary at 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)

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 2 Nov 2011
Copyright © CodeProject, 1999-2014
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