/// <summary> /// Inserts an user-created OrbitTexture into the cache for future reference /// </summary> /// <param name="texture">The instance of the OrbitTexture to insert in the cache</param> /// <param name="id">The proposed Id of the OrbitTexture</param> /// <returns>True if successfully inserted. False otherwise</returns> public static bool InsertInCache(OrbitTexture texture, string id) { if (IsInCache(id)) { return(false); } _Mutex.WaitOne(); // create the new node TextureNode node = new TextureNode(texture, id.ToLower()); node.Disposed += new EventHandler(Texture_Disposed); // expand the list TextureNode[] newList = new TextureNode[_Textures.Length + 1]; for (int i = 0; i < _Textures.Length; i++) { newList[i] = _Textures[i]; } newList[newList.Length - 1] = node; // finally replace the list _Textures = newList; _Mutex.ReleaseMutex(); //System.Diagnostics.Debug.WriteLine("InsertInCache(): Inserted " + id + " in cache"); return(true); }
/// <summary> /// Loads a Texture trying both TextureLoader and GDI as interim /// </summary> /// <param name="device">Direct3D Device to load the texture into</param> /// <param name="path">Path to load the texture from</param> /// <returns>A Direct3D Texture object. Null if failed</returns> public static OrbitTexture Load(Device device, string path) { Texture loadedTexture = null; // if file is an ICO file, use the FromIcon, otherwise proceed to the other ones if (System.IO.Path.GetExtension(path).Trim().ToLower() == ".ico") { loadedTexture = FromIcon(device, path); } // if not icon, try the other two possible methods if (loadedTexture == null) { // try TextureLoader loadedTexture = FromLoader(device, path); if (loadedTexture == null) { // this means TextureLoader failed. try GDI loadedTexture = FromGDI(device, path); } } // will return a texture of any of them succeeded. since both return null, will return null if failed. OrbitTexture orbitTexture = new OrbitTexture(loadedTexture, path); return(orbitTexture.GetReference()); }
private void DisposeBg() { // background if (this.BG != null) { this.BG.FreeReference(); this.BG = null; } }
private void CaptureThread() { // leave if not supported if (display.DeviceCaps.MaxTextureHeight < Screen.PrimaryScreen.Bounds.Height && display.DeviceCaps.MaxTextureWidth < Screen.PrimaryScreen.Bounds.Width) { return; } // destroy previous texture if (this.ScreenCapture != null) { this.ScreenCapture.FreeReference(); this.ScreenCapture = null; } // capture using (Bitmap MyBg = new Bitmap(SrcRect.Width, SrcRect.Height)) { try { // getting from clipboard using (Bitmap screen = (Bitmap)Clipboard.GetDataObject().GetData(typeof(Bitmap))) { // drawing on final file using (Graphics g = Graphics.FromImage((Image)MyBg)) { g.DrawImage(screen, new Rectangle(new Point(0, 0), SrcRect.Size), SrcRect.X, SrcRect.Y, SrcRect.Width, SrcRect.Height, System.Drawing.GraphicsUnit.Pixel); } } // creating texture this.ScreenCapture = OrbitTextureLoader.Load(display, MyBg); // validating this.CanDraw = true; // request update if (this.Paint != null) { this.Paint(this, new EventArgs()); } } catch (Exception) { // failed somewhere, so can't draw this.CanDraw = false; // delete vestigies if (ScreenCapture != null) { ScreenCapture.FreeReference(); ScreenCapture = null; } } // restoring the old clipboard RestoreOldClipboard(); } }
/// <summary> /// Disposes the ScreenGrabber class /// </summary> public void Dispose() { if (this.ScreenCapture != null) { this.ScreenCapture.FreeReference(); this.ScreenCapture = null; } if (this.RetrieveThread != null && this.RetrieveThread.IsAlive) { this.RetrieveThread.Abort(); } }
//Bitmap b; private void CaptureFromBitBlt() { try { // creating the bitmap using (Bitmap b = new Bitmap(SrcRect.Width, SrcRect.Height)) { // capturing using (Graphics g = Graphics.FromImage(b)) { IntPtr hdcScreen = Win32.GDI.GDIAPI.CreateDC("DISPLAY", null, null, IntPtr.Zero); IntPtr hdcG = g.GetHdc(); Win32.GDI.GDIAPI.BitBlt(hdcG, 0, 0, b.Width, b.Height, hdcScreen, SrcRect.X, SrcRect.Y, Win32.GDI.RasterOperation.SourceCopy); g.ReleaseHdc(hdcG); Win32.GDI.GDIAPI.DeleteDC(hdcScreen); } // destroy previous texture if (this.ScreenCapture != null) { this.ScreenCapture.FreeReference(); this.ScreenCapture = null; } // creating texture this.ScreenCapture = OrbitTextureLoader.Load(display, b); // validating this.CanDraw = true; this.Alpha = 255; // request update if (this.Paint != null) { this.Paint(this, new EventArgs()); } } } catch (Exception) { // failed somewhere, so can't draw this.CanDraw = false; this.Alpha = 0; // delete vestigies if (ScreenCapture != null) { ScreenCapture.FreeReference(); ScreenCapture = null; } } }
/// <summary> /// Removes an user-created OrbitTexture from the cache /// </summary> /// <param name="texture">The instance of the OrbitTexture to remove from the cache</param> /// <returns>True if successfully removed. False otherwise</returns> public static bool RemoveFromCache(OrbitTexture texture) { _Mutex.WaitOne(); TextureNode toRemove = null; for (int i = 0; i < _Textures.Length; i++) { if (_Textures[i].Texture == texture) { toRemove = _Textures[i]; } } if (toRemove != null) { // reduce the list TextureNode[] newList = new TextureNode[_Textures.Length - 1]; bool found = false; for (int i = 0; i < _Textures.Length; i++) { if (_Textures[i] != toRemove) { if (found) { newList[i - 1] = _Textures[i]; } else { newList[i] = _Textures[i]; } } else { found = true; } } // finally replace the list _Textures = newList; _Mutex.ReleaseMutex(); //System.Diagnostics.Debug.WriteLine("RemoveFromCache(): Removed " + toRemove.Id + " from cache"); return(true); } else { _Mutex.ReleaseMutex(); //System.Diagnostics.Debug.WriteLine("RemoveFromCache(): Texture not found in cache"); return(false); } }
/// <summary> /// Returns a reference to an existing OrbitTexture in the cache /// </summary> /// <param name="id">OrbitTexture id to return</param> /// <returns>The reference to the requested OrbitTexture. The instance was already notified of a new reference request - no need to call GetReference() on it</returns> public static OrbitTexture GetReference(string id) { _Mutex.WaitOne(); id = id.ToLower(); for (int i = 0; i < _Textures.Length; i++) { if (_Textures[i].Id == id) { OrbitTexture texture = _Textures[i].Texture.GetReference(); _Mutex.ReleaseMutex(); //System.Diagnostics.Debug.WriteLine("GetReference(): Got reference to " + id); return(texture); } } _Mutex.ReleaseMutex(); return(null); }
/// <summary> /// Loads a Texture from a GDI+ Bitmap /// </summary> /// <param name="device">Direct3D Device to load the texture into</param> /// <param name="sourceStream">Stream from which to create the texture</param> /// <returns>A OrbitTexture object. Null if failed</returns> public static OrbitTexture Load(Device device, System.IO.Stream sourceStream) { // try and load from the bitmap Texture texture = null; try { texture = new Texture(device, sourceStream, Usage.Dynamic, Pool.Default); } catch (Exception) { // return nothing if it fails return(null); } // create our OrbitTexture object which will house all the texture description parameters OrbitTexture orbitTexture = new OrbitTexture(texture); return(orbitTexture.GetReference()); }
/// <summary> /// Sets the Non Transparent background Texture /// </summary> /// <param name="Path">Path to load the Texture from</param> /// <returns>True if succeeded</returns> protected bool SetBg(string Path) { // dispose old background DisposeBg(); try { // use custom loader to load BG = OrbitTextureLoader.Load(display, Path); // only loaded if not null if (BG != null) { // get description and set path properly _BackgroundPath = Path; return(true); } } catch (Exception) {} // code will only reach this if failed loading _BackgroundPath = ""; return(false); }
public TextureNode(OrbitTexture texture, string id) { _Texture = texture; texture.Disposed += new EventHandler(Texture_Disposed); _Id = id; }