背景: android 和windows phone 都没有随手截屏功能,这点比iphone差了些。当然这是闲话,和我们的程序无关。
windows phone 平台上调用新浪微博开发接口,发送微博的函数为:
SdkShare sdkShare = new SdkShare { AccessToken = App.AccessToken, PicturePath = "TempJPEG.jpg", Message = this.messageTextBlock.Text };
其中有一项 PicturePath 。 它是在windows phone 中的 IsolatedStorageFile 存储空间里的。 所以要想发送一张含图片的 微博,首先要在windows phone 的独立存储空间里面有源图片。
下面进入正题。我想把当前屏幕画面,或者屏幕的部分内容保存为一张图片。可以用WriteableBitmap 来实现。
1 private void button1_Click(object sender, RoutedEventArgs e) 2 { 3 ScaleTransform transform = new ScaleTransform(); 4 transform.ScaleX = 1; 5 transform.ScaleY = 1; 6 7 System.Windows.Media.Imaging.WriteableBitmap we = new System.Windows.Media.Imaging.WriteableBitmap(this, transform); 8 img.Source = we; // img 是一个image控件 9 Save(we); 10 } 11 12 private void Save(System.Windows.Media.Imaging.WriteableBitmap we) 13 { 14 MemoryStream stream = new MemoryStream(); 15 we.SaveJpeg(stream,we.PixelWidth,we.PixelHeight,0,100); 16 stream.Seek(0,SeekOrigin.Begin); 17 IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
18 19 Stream istream = new IsolatedStorageFileStream("screen.jpg", FileMode.Create, fileStorage); 20 stream.CopyTo(istream); 21 stream.Close(); 22 istream.Close(); 23 25 }
WriteableBitmap(this, transform),this 是指把整个页面都截下来。当然可以截取部分,那么就把this改成this.gridName 等等。
img.Source = we;是直接用截屏的内容作为xaml的image控件的内容。 如果我想让image的内容指向isolatedStorage存储空间内的图片,则需要这样:
1 BitmapImage bmp = new BitmapImage(); 2 using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) 3 using (var stream = isolatedStorageFile.OpenFile("screen.jpg", FileMode.Open, FileAccess.Read)) 4 { 5 bmp.SetSource(stream); 6 } 7 img.Source = bmp;
用一个BitmapImage 承载图片的内容。
这里是源码 下载后去掉.jpg