.
在VB年代有专门的IMAGE数组控件,在.NET下却没有了这个控件,只能自己来实现。今天看DEVEXPRESS代码摘出来学习了一下
public
class
ImageCollectionEditor
{
private Image GetImageFromCollection(Image iml, int width, int height, int wIndex, int hIndex)
{
Bitmap res = new Bitmap(width, height);
res.SetResolution(iml.HorizontalResolution, iml.VerticalResolution);
Graphics g = Graphics.FromImage(res);
g.DrawImage(iml, 0, 0, new Rectangle(width * wIndex, height * hIndex, width, height), GraphicsUnit.Pixel);
g.Dispose();
return res;
}
/**//// <summary>
/// 获取IMAGE数组
/// </summary>
/// <param name="sourceImage">源图片</param>
/// <param name="vertical">图片方向</param>
/// <param name="InnerList">IMAGELIST控件</param>
public virtual void AddImageStrip(Image sourceImage, bool vertical,ref System.Windows.Forms.ImageList InnerList)
{
Size size = new Size (18,18);
int index = 0;
for (int i = vertical ? sourceImage.Height : sourceImage.Width; i > 0; i -= vertical ? size.Height : size.Width)
{
InnerList.Images.Add(GetImageFromCollection(sourceImage, size.Width, size.Height, vertical ? 0 : index, vertical ? index : 0));
index++;
}
}
{
private Image GetImageFromCollection(Image iml, int width, int height, int wIndex, int hIndex)
{
Bitmap res = new Bitmap(width, height);
res.SetResolution(iml.HorizontalResolution, iml.VerticalResolution);
Graphics g = Graphics.FromImage(res);
g.DrawImage(iml, 0, 0, new Rectangle(width * wIndex, height * hIndex, width, height), GraphicsUnit.Pixel);
g.Dispose();
return res;
}
/**//// <summary>
/// 获取IMAGE数组
/// </summary>
/// <param name="sourceImage">源图片</param>
/// <param name="vertical">图片方向</param>
/// <param name="InnerList">IMAGELIST控件</param>
public virtual void AddImageStrip(Image sourceImage, bool vertical,ref System.Windows.Forms.ImageList InnerList)
{
Size size = new Size (18,18);
int index = 0;
for (int i = vertical ? sourceImage.Height : sourceImage.Width; i > 0; i -= vertical ? size.Height : size.Width)
{
InnerList.Images.Add(GetImageFromCollection(sourceImage, size.Width, size.Height, vertical ? 0 : index, vertical ? index : 0));
index++;
}
}
MSDN针对DrawImage 的解释
Graphics
.
.
::
.DrawImage 方法 (Image,
array<Point
>
[]
()
[])
在指定位置并且按指定形状和大小绘制指定的 Image。
命名空间: System.Drawing
程序集: System.Drawing(在 System.Drawing.dll 中)
C#
public void DrawImage( Image image, Point[] destPoints )
参数
-
image
-
类型:
System.Drawing..::.Image
要绘制的 Image。
-
destPoints
-
类型:
array<
System.Drawing..::.Point
>
[]
()
[]