/// <summary> /// Create a cursor that can be set using SetUserCursor or SetShapeCursor. /// An ImageList image can be resized with LDImage.Resize. /// </summary> /// <param name="imageName">The file path or ImageList image.</param> /// <param name="xHotSpot">The x pixel to use as the hot spot, indexed from 0.</param> /// <param name="yHotSpot">The y pixel to use as the hot spot, indexed from 0.</param> /// <returns>A cursor.</returns> public static Primitive CreateCursor(Primitive imageName, Primitive xHotSpot, Primitive yHotSpot) { Type ShapesType = typeof(Shapes); Type ImageListType = typeof(ImageList); Dictionary <string, BitmapSource> _savedImages; BitmapSource img; string cursorName = ""; try { _savedImages = (Dictionary <string, BitmapSource>)ImageListType.GetField("_savedImages", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null); if (!_savedImages.TryGetValue((string)imageName, out img)) { imageName = ImageList.LoadImage(imageName); if (!_savedImages.TryGetValue((string)imageName, out img)) { return(cursorName); } } MethodInfo method = ShapesType.GetMethod("GenerateNewName", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase); cursorName = method.Invoke(null, new object[] { "Cursor" }).ToString(); Bitmap bmp = FastPixel.GetBitmap(img); Cursor cursor = createCursor(bmp, xHotSpot, yHotSpot); cursors[cursorName] = cursor; } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); } return(cursorName); }
/// <summary> /// Create a cursor file. /// </summary> /// <param name="imageName">The file path or ImageList image to create cursor from. Best results will be obtained from a square image.</param> /// <param name="cursorPath">The full path to save the cursor file (using extension *.cur).</param> /// <param name="size">The pixel size of cursor.</param> /// <param name="xHotSpot">Pixel from left of cursor hot spot, indexed from 0. /// For images where the aspect ratio is maintained, the xHotSpot is also scaled.</param> /// <param name="yHotSpot">Pixel from top of cursor hot spot, indexed from 0.</param> /// <returns></returns> public static Primitive CreateCursor(Primitive imageName, Primitive cursorPath, Primitive size, Primitive xHotSpot, Primitive yHotSpot) { try { Type ImageListType = typeof(ImageList); Dictionary <string, BitmapSource> _savedImages; BitmapSource img; _savedImages = (Dictionary <string, BitmapSource>)ImageListType.GetField("_savedImages", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null); if (!_savedImages.TryGetValue((string)imageName, out img)) { imageName = ImageList.LoadImage(imageName); if (!_savedImages.TryGetValue((string)imageName, out img)) { return("FAILED"); } } Bitmap bmp = FastPixel.GetBitmap(img); double scaleWidth = bSquare ? 1 : (double)bmp.Width / (double)bmp.Height; using (FileStream outStream = new FileStream(cursorPath, FileMode.Create)) { BinaryWriter bw = new BinaryWriter(outStream); bw.Write((byte)0); // 0-1 reserved (0) bw.Write((byte)0); bw.Write((short)2); // 2-3 image type, 1=Icon, 2=Cursor // bw.Write(icoType); bw.Write((short)1); // 4-5 number of images Bitmap bmpIcon = new Bitmap(bmp, new Size((int)(size * scaleWidth), size)); byte[] data; using (MemoryStream ms = new MemoryStream()) { bmpIcon.Save(ms, imageFormat); data = ms.ToArray(); } int offset = 6 + 16; bw.Write((byte)(size * scaleWidth)); // 0 image width (entry Image#1) bw.Write((byte)size); // 1 image height bw.Write((byte)0); // 2 number of colors (0 if the image does not use a color palette) bw.Write((byte)0); // 3 reserved (0) bw.Write((short)(xHotSpot * scaleWidth)); // 4-5 color planes (Ico: 0 or 1; Cur: horiz. coord. of xHotspot, # of pxl from left) bw.Write((short)yHotSpot); // 6-7 bits per pixel (Ico: bpPxl; Cur: vert. coord. of yHotspot, # of pxl from top) bw.Write((int)data.Length); // 8-11 size [Bytes] of image data bw.Write((int)offset); // 12-15 offset [Bytes] of Bmp/Png image data from beginning (6 + 16) bw.Write(data); // write image data (must contain the whole png data file) bw.Flush(); } return("SUCCESS"); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return("FAILED"); } }
public static ContextMenu getMenu(Dictionary <string, BitmapSource> _savedImages, Primitive items, Primitive images, int iconSize) { BitmapSource img; ContextMenu menu = new ContextMenu(); int itemCount = SBArray.GetItemCount(items); Primitive itemIndices = SBArray.GetAllIndices(items); for (int i = 1; i <= itemCount; i++) { string itemText = items[itemIndices[i]]; string imageName = images[itemIndices[i]]; // Add the item MenuItem menuItem = new MenuItem(); menuItem.Header = itemText; menuItem.Click += new RoutedEventHandler(_MenuClickEvent); menuItem.Tag = (string)(itemIndices[i]); // Creates the item image. if (imageName != "") { if (!_savedImages.TryGetValue(imageName, out img)) { imageName = ImageList.LoadImage(imageName); _savedImages.TryGetValue(imageName, out img); } if (null != img) { System.Windows.Controls.Image image = new System.Windows.Controls.Image(); image.Source = img; if (iconSize > 0) { Bitmap dImg = FastPixel.GetBitmap(img); System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(LDWebCam.ResizeAbort); dImg = (Bitmap)dImg.GetThumbnailImage(iconSize, iconSize, dummyCallback, IntPtr.Zero); image.Source = FastPixel.GetBitmapImage(dImg); } menuItem.Icon = image; } } menu.Items.Add(menuItem); } if (menu.Items.Count == 0) { return(null); } return(menu); }