如何在WPF下将ImageBrush对象的大图像调整为小尺寸? [英] How to resizing a large image of ImageBrush object to small under WPF?
问题描述
大家好,
我有一个ImageBrush,并在运行时在其ImageSource中设置了较大的图像,因此我想将其调整为较小的图像.
换句话说,图像很大并在运行时加载,实际上我想在WPF设施下将自身大小调整为小图像,而不使用"System.Drawing"命名空间. />
我也使用了这种方法,但是对此我的问题有一个错误:
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;
}
因为我想像以下程序一样调整myImageBrush的大小:
Because I want resizing the myImageBrush like this program:
myImageBrush.ImageSource = CreateBitmapSourceFromVisual(100, 100, myImageBrush);
任何帮助请...
在此先谢谢您.
Any help pls...
Thanks in advance.
推荐答案
您也应该能够在运行时创建较小的图像.您很可能需要根据需要设置DecodePixelWidth
和DecodePixelHeight
.
示例:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.decodepixelheight.aspx [
You should be able to create the smaller image at run time too. You''d most likely need to setDecodePixelWidth
andDecodePixelHeight
accordingly.
Example:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.decodepixelheight.aspx[^]
使用 ^ ]:
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.
这篇关于如何在WPF下将ImageBrush对象的大图像调整为小尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!