简体   繁体   English

在WP7上不断更改图像的最佳性能方法是什么?

[英]What's best performance way to constantly change image on WP7?

I'm trying to make my own type of remote desktop for WP7. 我正在尝试为WP7制作自己的类型的远程桌面。 I have a WCF service that returns an image on what's on the target machine's screen. 我有一个WCF服务,该服务会在目标计算机的屏幕上返回图像。

Here's the WCF Server Code: 这是WCF服务器代码:

    // Method to load desktop image
Bitmap image = new Bitmap( ViewSize.Width, ViewSize.Height );
Graphics g = Graphics.FromImage( image );

g.CopyFromScreen( Position.X, Position.Y, 0, 0, ViewSize );

g.Dispose( );
return image;

// Convert image to byte[] which is returned to client
using ( MemoryStream ms = new MemoryStream( ) )
{
    Bitmap image = screenGrabber.LoadScreenImage( );
    image.Save( ms, ImageFormat.Jpeg );
    imageArray = ms.ToArray( );
}

Here's the code for the WP7 client: 这是WP7客户端的代码:

    MemoryStream stream = new MemoryStream( data );
BitmapImage image = new BitmapImage( );

image.SetSource( stream );
BackgroundImage.Source = image;

The BackgroundImage variable is an Image control. BackgroundImage变量是一个Image控件。

I'm noticing this freeze on the emulator after a short while, and will eventually crash from an OutOfMemoryException. 我注意到不久后在模拟器上冻结,最终将因OutOfMemoryException崩溃。 This is already pretty slow ( images show up a good half second later than what's on the screen ), and I'm wondering if there's a better/faster way of doing this? 这已经很慢了(图像比屏幕上的显示晚了半秒钟),我想知道是否有更好/更快的方法? Any help would be great. 任何帮助都会很棒。 Thanks in advance. 提前致谢。

I think I can shed some light on your OutOfMemoryException. 我想我可以阐明您的OutOfMemoryException。 Are you aware of the IDisposable interface? 您是否知道IDisposable接口? The MemoryStream type is IDisposable so you must invoke Dispose on it to ensure that it is garbage collected and any resources it holds are freed. MemoryStream类型是IDisposable,因此必须在其上调用Dispose以确保已对其进行垃圾回收并释放了其拥有的所有资源。 Your code should be as follows: 您的代码应如下所示:

using(MemoryStream stream = new MemoryStream( data ))
{
  BitmapImage image = new BitmapImage( );
  image.SetSource( stream );
}

Regarding performance, real remote desktop (RDP) applications do not send the entire screen image every time something changes, they send partial updates in order to minimize bandwidth usage. 关于性能,真正的远程桌面(RDP)应用程序不会在每次更改时发送整个屏幕图像,而是发送部分更新以最大程度地减少带宽使用。

If you are going to go for a full screen refresh each time, make sure it is suitably scaled and compressed. 如果每次都要进行全屏刷新,请确保已适当缩放和压缩它。

ColinE is right but there is a nuance. ColinE是正确的,但有细微差别。 If you use his code it will automatically dispose the image once it has been set as the source. 如果使用他的代码,将其设置为源后,它将自动处理该图像。 The result of this will either be an error indicating that you have trried to update the UI from a non UI thread or result in a problem when trying to visualize an allready disposed Bitmap. 结果可能是错误,表明您已尝试从非UI线程更新UI,或者在尝试可视化已准备就绪的位图时导致问题。

Instead I would opt to keep a refrence to the current image, then change the image then use the refrence to the previous current image to dispose that image. 相反,我会选择保留对当前图像的引用,然后更改图像,然后使用对先前的当前图像的引用来处置该图像。 Don't have access to an IDE atm but something alongst the lines of (assuming the code runs in the UI thread, otherwise you will have to also ensure that the code is evoked there). 没有访问IDE atm的权限,而是访问其他东西的权限(假设代码在UI线程中运行,否则,您还必须确保代码在此处被唤醒)。

MemoryStream stream = new MemoryStream( data ); MemoryStream流=新的MemoryStream(data); BitmapImage image = new BitmapImage( ); BitmapImage图片=新的BitmapImage();
image.SetSource( stream ); image.SetSource(stream);

IDisposable toDispose = (IDisposable) BackgroundImage.Source; IDisposable toDispose =(IDisposable)BackgroundImage.Source;

BackgroundImage.Source = image; BackgroundImage.Source =图片;

toDispose.Dispose(); toDispose.Dispose();

Also if you dan't want to reinvent to much of the wheel have a look at VNC, VNC# is a libary for it and it gives you a reasonable understanding of how others have done desktop remoting before 另外,如果您不想重蹈覆辙,请查看VNC, VNC#是它的库,它使您可以合理地了解其他人之前如何进行桌面远程处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM