.net c#捕获桌面并用作纹理?

Capture desktop and use as texture?
2021-03-30
  •  译文(汉语)
  •  原文(英语)

我已经阅读了很多有关在Unity3D中拍摄屏幕截图的示例,但是它们都解释了"游戏中"屏幕截图.我想在桌面上拍摄应用程序窗口的屏幕截图,并将其作为纹理绘制到GameObject上.

知道我该怎么做吗?我只使用过Unity3D几次,因此希望能获得一些指导,以了解这是否可行以及如何实现.

我知道我可以编写一些常规的C#应用​​程序,将Windows的输出捕获到文件中并在Unity3D中读取图像.但是,我不希望写入磁盘.

目前,我所能做的就是获取"游戏中"屏幕截图...

void LateUpdate()
{
    //I know that I could load an image from file here...
    Texture2D tex = new Texture2D(Screen.width, Screen.height);
    tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    tex.Apply();
    display.material.mainTexture = tex;
}
速聊1:
制作屏幕截图,将其保存到资产文件夹,为其创建材料,分配给您想要分配给它的任何内容?还是我完全误会了?
速聊2:
抱歉,我已经考虑过了,正在将其添加到帖子中.请查看更新.
速聊3:
我仍然不了解您尝试归档的内容.^^如果只是屏幕截图,请制作屏幕截图(如在使用键盘上的printscreen键时一样).还是您想要某种桌面应用程序的现场直播?
速聊4:
我想捕获桌面上所有打开的窗口(MS Word,Excel等),然后将这些捕获显示在游戏对象上.我不想写入磁盘.桌面应用程序的实时流差不多.
速聊5:
他想要整个设备的屏幕截图,而不是统一运行游戏的屏幕截图.例如,当统一应用未全屏运行时,他希望能够拍摄屏幕上显示的内容.屏幕上同时显示统一应用和其他内容.
解决过程1

我认为您应该使用DLL.
在这里看一下这个答案.

他解释了如何在Visual C#中捕获Window句柄.我认为您可以找到一个桌面句柄(在Win7 +上)并捕获它,然后使用您的DLL将其发送给unity.

I have read a lot of examples about taking screenshots in Unity3D, but they all explain "in-game" screenshots. I would like to take a screenshot of an application window on my desktop and paint it onto a GameObject as a texture.

Any idea how I can go about doing this? I've only used Unity3D a few times and so would appreciate some guidance on if this is possible or not and how I might achieve this.

I know that I can write some regular C# application that will capture the outputs of windows to file and read the images in Unity3D. However, I would prefer not to write to disk.

At the moment, all I am able to do is get an "in-game" screenshot...

void LateUpdate()
{
    //I know that I could load an image from file here...
    Texture2D tex = new Texture2D(Screen.width, Screen.height);
    tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    tex.Apply();
    display.material.mainTexture = tex;
}
Talk1:
Make a screenshot, save it to your asset folder, create a material for it, assign to whatever you want it to assign to? Or do i misunderstand completely?
Talk2:
Sorry, I have already considered that and was adding it to the post. Please see the update.
Talk3:
I still dont get what you try to archieve ^^ If its just a screenshot, just make a screenshot (as in use the printscreen key on your keyboard). Or do you want some sort of live steam of some desktop app?
Talk4:
I want to capture all open windows on the desktop (MS Word, Excel, etc...) and display those captures on Game Objects. I do not want to write to disk. Live stream of desktop applications is pretty much it.
Talk5:
He wants a screenshot of the whole device instead of screen shot of the game running in unity. For example, when unity app is not running full screen, he want to be able to take picture of what displayed on the screen. Both unity app and other things on the screen.
Solutions1

I think you should use DLLs.
Take a look here at this SO answer.

He explains how to capture a Window handle in Visual C#. I think you can find a Desktop handle (on Win7+) and capture it, then send it to unity using your DLL.

转载于:https://stackoverflow.com/questions/37092185/capture-desktop-and-use-as-texture

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入