</pre>WriteableBitmap我想大家並不陌生吧,它是一個基於內存的圖像管理類,大家可以把它認爲圖像是一堆存儲在內存中的數據,這些數據可由WriteableBitmap管理和分配。 這裏我就給大家講一些關於WriteableBitmap的一些使用技巧: 實現自繪 衆所周知,目前爲止,微軟還沒有開放自繪接口,如果你真的想在界面上自已繪製一個字符串,都有些困難呢。下面的代碼正是使用WriteableBitmap來實現自繪的方案 <pre name="code" class="csharp">private void RenderString(WriteableBitmap bitmap, string stringToRender) { TextBlock textBlock = new TextBlock(); textBlock.Text = stringToRender; //設置 font, size,等等 bitmap.Render(textBlock, null); bitmap.Invalidate(); }
怎麼樣,很簡單吧,他是通過將TextBlock中的文本繪製到WriteableBitmap來實現的,在這裏我發揮一下,那不就可以通過這個方法,來實現一個圖片水印的功能麼,趕快去試試吧
順便說一點,這裏我要介紹一個更強大的開源的庫writeablebitmapex,如果大家想要繪製更復雜的的圖像如:點,線,曲線,陰影,形狀,以及實現一些常用的圖像數據處理功能,那麼這個庫將是大家
最好的選擇。
圖像的縮放存儲
如果你想將一張圖片改變大小,那麼你可以用以下的方法去實現
WriteableBitmap resizedImage = new WriteableBitmap(imageToResize);//imageToResize is BitmapImage
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
{
double maxHeight = newWidth;
double maxWidth = newHeight;
double scaleX = 1;
double scaleY = 1;
if (pixHt > maxHeight)
scaleY = maxHeight / pixHt;
if (pixWt > maxWidth)
scaleX = maxWidth / pixWt;
double scale = Math.Min(scaleY, scaleX);
int newWidth1 = Convert.ToInt32(pixWt * scale);
int newHeight1 = Convert.ToInt32(pixHt * scale);
resizedImage.SaveJpeg(isfs, newWidth1, newHeight1, 0, 70);
isfs.Close();
}
}
在這裏,imageToResize就是你輸入的圖像,你可以將它存儲爲目標大小的圖像文件
對控件(全屏)進行截圖
在很多應用中,如果要對當前頁面進行截圖,應該怎麼辦呢,這時WriteableBitmap就能幫助到你了。
把頁面截圖,並保存到內存
WriteableBitmap wb = new WriteableBitmap(UiRoot, null);//將UI頁面的根元素傳入,可將當面頁面的截圖保存到WriteableBitmap
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms, myWidth, myHeight, 0, 100);//保存到內存MemoryStream
BitmapImage bmp = newBitmapImage(); //把截圖轉化爲BitmapImage
bmp.SetSource(ms);
using (var isoFileStream =newIsolatedStorageFileStream("myPicture.jpg",FileMode.OpenOrCreate,IsolatedStorageFile.GetUserStoreForApplication()))
{
wb.SaveJpeg(isoFileStream, myWidth, myHeight,0,100); //把截圖存儲到獨立存儲
}
將存儲在Sql數據庫的圖片二進制數據載入到內存
有些時候圖片數據是以二進制數據保存到sqlite數據庫中的,下面將是,如何把這些二進制數據還原成圖像格式
public static byte[] ConvertToBytes(String imageLocation)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri(imageLocation, UriKind.RelativeOrAbsolute));
BinaryReader binary = new BinaryReader(sri.Stream);
byte[] imgByteArray = binary.ReadBytes((int)(sri.Stream.Length));
binary.Close();
binary.Dispose();
return imgByteArray;
}
public static WriteableBitmap ConvertToImage(Byte[] inputBytes)
{
MemoryStream ms = new MemoryStream(inputBytes);
WriteableBitmap img = new WriteableBitmap(400, 400);
img.LoadJpeg(ms);
return (img);
}
實現鼠標移動到地圖上的地區,實現區域選中(白色)與未選中(藍色)
前臺
<!--白色地圖-->
<Image x:Name="ProvinceImageM" Stretch="Fill"
MouseLeftButtonUp="ProvinceImage_MouseLeftButtonUp"
MouseMove="ProvinceImage_MouseMove"></Image>
<!--藍色地圖-->
<Image x:Name="ProvinceImage" Stretch="Fill"
MouseLeftButtonUp="ProvinceImage_MouseLeftButtonUp"
MouseMove="ProvinceImage_MouseMove"></Image>
後臺
(主要思路是獲取當前鼠標的像素,然後轉換成Byte 判斷RGP的值)
public void ProvinceImage_MouseMove(object sender, MouseEventArgs e)
{
#region 判斷鼠標移動位置
try
{
WriteableBitmap bitmap = null;
Point myPoint = new Point();
if (sender as ProvinceThumbnail != null)
{
myPoint = e.GetPosition((sender as ProvinceThumbnail).ProvinceImage);
if (myPoint.X < 0) return; //當X小於零,圖片事件會相應錯誤。
bitmap = new WriteableBitmap((sender as ProvinceThumbnail).ProvinceImage, null); // my image 圖片控件名
}
else
{
return;
}
if (myPoint.Y <= bitmap.PixelHeight && myPoint.X <= bitmap.PixelWidth)
{
int pixelLocation = bitmap.PixelWidth * int.Parse(myPoint.Y.ToString()) + int.Parse(myPoint.X.ToString());
int pixel = bitmap.Pixels[pixelLocation];
byte[] pixBytes = BitConverter.GetBytes(pixel);
if (pixBytes[0] != 0 || pixBytes[1] != 0 || pixBytes[2] != 0) // 本圖
{
ProvinceImage.Visibility = Visibility.Collapsed;
ProvinceImageM.Visibility = Visibility.Visible;
}
else if (pixBytes[0] == 0 && pixBytes[1] == 0 && pixBytes[2] == 0) //透明
{
ProvinceImage.Visibility = Visibility.Visible;
ProvinceImageM.Visibility = Visibility.Collapsed;
}
}
}
catch (Exception ex)
{
//do nothing
}
#endregion
}