代码改变世界

Silverlight杂记- 图片及WriteableBitmap的使用(画分形1)

2010-12-26 17:40  撞破南墙  阅读(2219)  评论(1编辑  收藏  举报

 

支持的图片格式

image

 

从一个UI中获取为图片

 

WriteableBitmap bmp = new WriteableBitmap(SP1, null); 
    img3.Source 
= bmp;

 

 

7

画图

先看效果吧,还是挺漂亮的

image

 

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 = 0double 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