代码改变世界

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

  撞破南墙  阅读(2267)  评论(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

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2009-12-26 自写的 c# 锚点,前端显示 读书 记事本(一)
点击右上角即可分享
微信分享提示