Skip to content

Instantly share code, notes, and snippets.

@NegishiTakumi
Created December 13, 2015 02:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NegishiTakumi/396ff765768c04021f0c to your computer and use it in GitHub Desktop.
Save NegishiTakumi/396ff765768c04021f0c to your computer and use it in GitHub Desktop.
//https://gist.github.com/NegishiTakumi/69b165587fd71f4ab8a3 のマウスポインタ付き描画
using UnityEngine;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using ChangeDataToBytesFromImage;
using System.Threading;
using System.Threading.Tasks;
using SpicyPixel;
using SpicyPixel.Threading;
using SpicyPixel.Threading.Tasks;
using System.Windows.Forms;
public class DesktopDisplay : ConcurrentBehaviour
{
[DllImport("System.Drawing.dll")]
private static extern void Func();
public int ClipWidth = 1920;
public int ClipHeight = 1080;
public int TextureWidth = 1920;
public int TextureHeight = 1080;
private Material targetMaterial;
private Texture2D tex;
private Thread thread;
bool isThreadEnd = false;
public virtual void Awake()
{
base.Awake();
targetMaterial = GetComponent<Renderer>().material;
thread = new Thread(Thread1);
thread.Start();
}
void Thread1()
{
while (true)
{
Image img = GetCaptureImage(new Rectangle(0 ,0 , ClipWidth, ClipHeight));
var imageBytes = ChangeDataToBytesFromImage.ConvertImageData.ConvertImageToBytes(img);
img.Dispose();
taskFactory.StartNew(() => UpdateTexture(imageBytes));
if (isThreadEnd) break;
}
}
void UpdateTexture(byte[] imageBytes)
{
if (tex == null)
{
tex = new Texture2D(TextureWidth, TextureHeight);
}
tex.LoadImage(imageBytes);
targetMaterial.mainTexture = tex;
}
private Image GetCaptureImage(Rectangle rect)
{
// 指定された範囲と同サイズのBitmapを作成する
Image img = new Bitmap(
rect.Width,
rect.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Bitmapにデスクトップのイメージを描画する
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img))
{
var cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
var curPoint = System.Windows.Forms.Cursor.Position;
var hotspot = cursor.HotSpot;
g.CopyFromScreen(
rect.X,
rect.Y,
0,
0,
rect.Size,
CopyPixelOperation.SourceCopy);
var p = new Point(curPoint.X - hotspot.X, curPoint.Y - hotspot.Y);
g.FillEllipse(Brushes.Red,p.X,p.Y,15,15);
}
return img;
}
void OnApplicationQuit()
{
isThreadEnd = true;
thread.Abort();
}
}
@NegishiTakumi
Copy link
Author

//https://gist.github.com/NegishiTakumi/69b165587fd71f4ab8a3 のマウスポインタ付き描画

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment