代码改变世界

Silverlight 工具类 ImageClipUtil

2010-09-20 21:45  平凡主义  阅读(542)  评论(2编辑  收藏  举报

  在实际开发过程中往往会将很多张图片素材放在一张图片中进行下载,其中的原因大家都应该明白.

  在刚学Siverlight 的时候,我是通过Clip 进行剪切,再进行定位.而这样的话定位十分麻烦,所以我写了一个工具类,现在分享给大家.

  1.配置

  

代码
<UIConfig>
<UI name="Image" url="CDGameResource/Images/Image.png">
<Image Name="Image_1" Width="433" Height="351" X="0" Y="0" />
<Image Name="Image_2" Width="105" Height="22" X="0" Y="352" />
 </UI>
<UI name="Image1" url="CDGameResource/Images/Image1.png">
   
<Image Name="Image1_1" Width="433" Height="351" X="0" Y="0" />
   
<Image Name="Image1_2" Width="105" Height="22" X="0" Y="352" />
 </UI>
<UIConfig>

 这里对图片进行配置 UI   节点代表每一张图片 而Image 节点 的命名是 UI的名称加自己定义的图片元素的名称
 

  2. 进行解析

 

图片帮助类
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.Diagnostics;

namespace CDGame.utils
{
public static class ImageClipUtil
{

/// <summary>
/// 图片字典
/// </summary>
public static Dictionary<string, Image> UIList = new Dictionary<string, Image>();
/// <summary>
/// 图片解析完毕后触发的事件
/// </summary>
public static event EventHandler ImageCompletedEventHandler;
/// <summary>
/// 用来转换的图片
/// </summary>
private static Image spirit;
/// <summary>
/// 图片配置
/// </summary>
private static XDocument xDoc;
/// <summary>
/// 图片配置节点集合
/// </summary>
private static List<XElement> nodes;

public static XDocument XDoc
{
get { return ImageClipHelper.xDoc;}
set {

spirit
= new Image();

ImageClipHelper.xDoc
= value;
nodes
= new List<XElement>();

foreach (var x in xDoc.Descendants("UI"))
{
nodes.Add(x);
}
DownStart(
0);

}
}
/// <summary>
/// 获得图片
/// </summary>
/// <param name="ImageNamge"></param>
/// <returns></returns>
public static Image GetImage(string ImageNamge)
{
Image img
= UIList[ImageNamge];
return new Image() { Source = img.Source, Width = img.Width, Height = img.Height };
}

static void DownStart(int index)
{


WebClient wc
= new WebClient();
//加载中
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler((o, e) =>
{
//这里可以给用户一些提示
});
XElement xe
= nodes[index];
string Name = xe.Attribute("name").Value;
string Url = xe.Attribute("url").Value;
wc.OpenReadAsync(
new Uri(Url, UriKind.RelativeOrAbsolute),Name);



//异步读取完成
wc.OpenReadCompleted += new OpenReadCompletedEventHandler((o, e) =>
{
if (e.Error != null)
throw new Exception(e.Error.ToString());
//设置图片源
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(e.Result);
spirit.Source
= bitmapImage;
//进行剪裁

foreach (var n in nodes[index].Descendants("Image"))
{
int width = Convert.ToInt32(n.Attribute("Width").Value);
int height = Convert.ToInt32(n.Attribute("Height").Value);
int x = Convert.ToInt32(n.Attribute("X").Value);
int y = Convert.ToInt32(n.Attribute("Y").Value);
string imageName = n.Attribute("Name").Value;
SetImageItem(width, height, x, y, imageName);

}
index
++;
if (index < nodes.Count)
{
DownStart(index);
}
else
{
bitmapImage
= null;

if (ImageCompletedEventHandler != null)
{
ImageCompletedEventHandler(
null, null);
}
}

});


}
#region 生成一个背景重复的图片
/// <summary>
/// 输入图片名称,获得一个垂直或者水平的重复图片的背景图
/// </summary>
/// <param name="length">长度</param>
/// <param name="orientation">重复方向</param>
/// <param name="imageName">图片名称</param>
/// <returns>返回图片</returns>
public static Image GetRepeatImage(int length, Orientation orientation, string imageName)
{
Image image
= UIList[imageName];
return GetRepeatImage(length, orientation, image);
}

/// <summary>
/// 传入一个图片,获得一个垂直或者水平的重复图片的背景图
/// </summary>
/// <param name="length">长度</param>
/// <param name="orientation">重复方向</param>
/// <param name="image">图片</param>
/// <returns>返回图片</returns>
public static Image GetRepeatImage(int length, Orientation orientation, Image image)
{

WriteableBitmap source
= new WriteableBitmap((BitmapSource)image.Source);
int width;
int height;
if (orientation == Orientation.Horizontal)
{
width
= length;
height
=(int)image.Height;
}
else
{
width
= (int)image.Width;
height
= length;
}
WriteableBitmap final
= new WriteableBitmap(width, height);
for (int x = 0; x < final.PixelWidth; x++)
{
for (int y = 0; y < final.PixelHeight; y++)
{
int tiledX = (x % source.PixelWidth);
int tiledY = (y % source.PixelHeight);

final.Pixels[y
* final.PixelWidth + x] = source.Pixels[tiledY * source.PixelWidth + tiledX];
}
}

return new Image() { Source = (ImageSource)final, Width = width, Height = height };

}

/// <summary>
/// 传入一个图片,获得一个固定大小的的背景图
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="imageName">图片名称</param>
/// <returns>返回图片</returns>
public static Image GetRepeatImage(int width, int height, Image image)
{

WriteableBitmap source
= new WriteableBitmap((BitmapSource)image.Source);


WriteableBitmap final
= new WriteableBitmap(width, height);
for (int x = 0; x < final.PixelWidth; x++)
{
for (int y = 0; y < final.PixelHeight; y++)
{
int tiledX = (x % source.PixelWidth);
int tiledY = (y % source.PixelHeight);

final.Pixels[y
* final.PixelWidth + x] = source.Pixels[tiledY * source.PixelWidth + tiledX];
}
}

return new Image() { Source = (ImageSource)final, Width = width, Height = height };

}


#endregion


public static Image GetImage(int width, int height, int x, int y, Image images)
{

WriteableBitmap final
= new WriteableBitmap(width, height);

WriteableBitmap source
= new WriteableBitmap((BitmapSource)images.Source);
//不用GUP 缓存
for (int i = 0; i < height; i++)
{
int index = (y + i) * source.PixelWidth + x;
for (int j = 0; j < width; j++)
{

final.Pixels[i
* width + j] = source.Pixels[index + j];
}
}

//可以用下载这个注释的内容,同样可以得到图片,但是会被GUP 缓存
//WriteableBitmap wb = new WriteableBitmap(width, height);
//wb.Render(spirit, new TranslateTransform() { X = -x, Y = -y });
//wb.Invalidate();
Image img = new Image() { Source = (ImageSource)final, Width = width, Height = height, CacheMode = null };
return img;

}
public static void SetImageItem(int width, int height, int x, int y, string imageName)
{

WriteableBitmap final
= new WriteableBitmap(width, height);
WriteableBitmap source
= new WriteableBitmap((BitmapSource)spirit.Source);

for (int i = 0; i < height; i++)
{
int index = (y+i) * source.PixelWidth + x;
for (int j = 0; j < width; j++)
{

final.Pixels[i
*width+j] = source.Pixels[index+j];
}
}


//WriteableBitmap wb = new WriteableBitmap(width, height);
//wb.Render(spirit, new TranslateTransform() { X = -x, Y = -y });
//wb.Invalidate();
Image img = new Image() { Source = (ImageSource)final, Width = width, Height = height, CacheMode = null };
UIList.Add(imageName, img);

}




}
}

 

 

3.下载xml 后传给ImageClipUtil

 

使用
WebClient webClient = new WebClient();
webClient.OpenReadAsync(
new Uri("xml 地址",UriKind.Relative));
webClient.OpenReadCompleted
+=(s,e)=>
{
XDocument doc
= XDocument.Load(e.Result);
ImageClipUtil.XDoc
= doc;
};
ImageClipUtil.ImageCompletedEventHandler
+= (s, e) =>
{
//进行自己需要的操作比如
Image img = ImageClipHelper.GetImage("配置中的Image 名称");
};

 

 

 

 这里注意如果 需要使用的图片需要多次使用的话 那么 得到Image 的时候用  Image img = ImageClipUtil.GetImage("xxx").Source;

 

希望对大家有一些帮助!

 

那啥,请勿Ctrl +c Ctrl+v .

人活二十几,全靠懂的起哈!