Silverlight杂记- 图片及WriteableBitmap的使用(画分形1)
2010-12-26 17:40 撞破南墙 阅读(2265) 评论(1) 编辑 收藏 举报
支持的图片格式
从一个UI中获取为图片
WriteableBitmap bmp = new WriteableBitmap(SP1, null);
img3.Source = bmp;
img3.Source = bmp;
画图
先看效果吧,还是挺漂亮的
private void Draw() {
int width = 1024;
int height = 768;
int[] colorTable = new int[256];
for (int i = 0; i < 256; i++) {
Color c = Color.FromArgb(
0xFF, (byte)(255 - i), (byte)(255 - i), (byte)(255));
colorTable[i] = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
}
WriteableBitmap bmp = new WriteableBitmap(width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double zoom = 300;
double x0 = 0; double y0 = 0;
double cx = (x - width / 2) / zoom;
double cy = (y - height / 2) / zoom;
int iteration = 0;
int maxIterations = 1000;
while (x0 * x0 + y0 * y0 <= 4 && iteration < maxIterations) {
double xtemp = x0 * x0 - y0 * y0 + cx;
y0 = 2 * x0 * y0 + cy;
x0 = xtemp;
iteration++;
}
if (iteration == maxIterations) {
bmp.Pixels[(y * width) + x] =
colorTable[colorTable.GetUpperBound(0)];
} else {
bmp.Pixels[(y * width) + x] =
colorTable[iteration % colorTable.Length];
}
}
}
image2.Source = bmp;
}
int width = 1024;
int height = 768;
int[] colorTable = new int[256];
for (int i = 0; i < 256; i++) {
Color c = Color.FromArgb(
0xFF, (byte)(255 - i), (byte)(255 - i), (byte)(255));
colorTable[i] = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
}
WriteableBitmap bmp = new WriteableBitmap(width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double zoom = 300;
double x0 = 0; double y0 = 0;
double cx = (x - width / 2) / zoom;
double cy = (y - height / 2) / zoom;
int iteration = 0;
int maxIterations = 1000;
while (x0 * x0 + y0 * y0 <= 4 && iteration < maxIterations) {
double xtemp = x0 * x0 - y0 * y0 + cx;
y0 = 2 * x0 * y0 + cy;
x0 = xtemp;
iteration++;
}
if (iteration == maxIterations) {
bmp.Pixels[(y * width) + x] =
colorTable[colorTable.GetUpperBound(0)];
} else {
bmp.Pixels[(y * width) + x] =
colorTable[iteration % colorTable.Length];
}
}
}
image2.Source = bmp;
}
WriteableBitmap 扩展阅读:
http://www.cnblogs.com/webabcd/archive/2009/08/27/1554804.html
作者:撞破南墙
出处:http://www.cnblogs.com/facingwaller/
关于作者:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。