Silverlight寫圖像功能特點(diǎn)詳解
Silverlight對(duì)于圖像的處理是比較強(qiáng)大的。主要可以通過(guò)Bitmap API來(lái)實(shí)現(xiàn)Silverlight寫圖像。在這里我們將會(huì)為大家詳細(xì)介紹這一功能的實(shí)現(xiàn)方式,希望能對(duì)大家有所幫助,提高編程效率。#t#
新版的Bitmap API支持從寫每個(gè)像素的值來(lái)創(chuàng)建自己的圖像。這個(gè)用來(lái)支持生成Bitmap的類叫做WriteableBitmap,繼承自BitmapSource類。這個(gè)類位于System.Windows.Media.Imaging名字空間中,其函數(shù)成員包括:
- public sealed class WriteableBitmap
: BitmapSource - {
- public WriteableBitmap(BitmapSource source);
- public WriteableBitmap(int pixelWidth,
int pixelHeight, PixelFormat format); - public int this[int index] { get; set; }
- public void Invalidate();
- public void Lock();
- public void Render(UIElement
element, Transformtransform); - public void Unlock();
- }
可以看出我們可以通過(guò)兩種Silverlight寫圖像的形式來(lái)實(shí)例化這個(gè)WriteableBitmap。一個(gè)是通過(guò)傳入已經(jīng)初始化了的BitmapSource,另外一個(gè)是通過(guò)輸入圖像高度和寬度以及像素類型(有Bgr32和Pbgra32兩種,后面一種可以創(chuàng)建半透明圖像)。第5行的索引this[int index]可以用來(lái)讀或取像素點(diǎn)。
寫一個(gè)WriteableBitmap的流程是這樣的。實(shí)例化WriteableBitmap,調(diào)用Lock方法,寫像素點(diǎn),調(diào)用Invalidate方法,最后是調(diào)用Unlock方式來(lái)釋放所有的Buffer并準(zhǔn)備顯示。
如下文所示以描點(diǎn)的方式構(gòu)建了整個(gè)Bgr32圖像
- private WriteableBitmap BuildTheImage
(int width, int height)- {
- WriteableBitmap wBitmap=new Writeable
Bitmap(width,height,PixelFormats.Bgr32);- wBitmap.Lock();
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- byte[] brg = new byte[4];
- brg[0]=(byte)(Math.Pow(x,2) % 255);
//Blue, B- brg[1] = (byte)(Math.Pow(y,2)
% 255); //Green, G- brg[2] = (byte)((Math.Pow(x, 2) + Mat
h.Pow(y, 2)) % //Red, R- brg[3] = 0;
- int pixel = BitConverter.
ToInt32(brg, 0);- wBitmap[y * height + x] = pixel;
- }
- }
- wBitmap.Invalidate();
- wBitmap.Unlock();
- return wBitmap;
- }
Silverlight寫圖像的實(shí)現(xiàn)方法就為大家介紹到這里。

















