Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can rotate your cursor using the System.Drawing.Icon class from WinForms in combination with WPF's bitmap rotation ability.</p> <p>The way to do this is to load the icon, convert it to a BitmapSource, use Image and RenderTargetBitmap to rotate it, convert it back to an Icon, save it, and finally update bytes 2, 10, and 11 that make it a .cur instead of a .ico.</p> <p>Here's what the code looks like:</p> <pre><code>public Cursor GetRotatedCursor(byte[] curFileBytes, double rotationAngle) { // Load as Bitmap, convert to BitmapSource var origStream = new MemoryStream(curFileBytes); var origBitmap = new System.Drawing.Icon(origStream).ToBitmap(); var origSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(origBitmap.GetHBitmap()); // Construct rotated image var image = new Image { BitmapSource = origSource, RenderTransform = new RotateTransform(rotationAngle) }; // Render rotated image to RenderTargetBitmap var width = origBitmap.Width; var height = origBitmap.Height; var resultSource = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); resultSource.Render(image); // Convert to System.Drawing.Bitmap var pixels = new int[width*height]; resultSource.CopyPixels(pixels, width, 0); var resultBitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPargb); for(int y=0; y&lt;height; y++) for(int x=0; x&lt;width; x++) resultBitmap.SetPixel(x, y, Color.FromArgb(pixels[y*width+x])); // Save to .ico format var resultStream = new MemoryStream(); new System.Drawing.Icon(resultBitmap.GetHIcon()).Save(resultStream); // Convert saved file into .cur format resultStream.Seek(2); resultStream.WriteByte(curFileBytes, 2, 1); resultStream.Seek(10); resultStream.WriteByte(curFileBytes, 10, 2); resultStream.Seek(0); // Construct Cursor return new Cursor(resultStream); } </code></pre> <p>If you want to avoid the loop, you can replace it with a small bit of usafe code to call the System.Drawing.Bitmap constructor that takes initialization data:</p> <pre><code> fixed(int* bits = pixels) { resultBitmap = new System.Drawing.Bitmap(width, height, width, System.Drawing.Imaging.PixelFormat.Format32bppPargb, new IntPtr(bits)); } </code></pre> <p>You'll need to call this every time your TextBox rotation changes. This can be done either from the code that rotates your TextBox, or from a PropertyChangedCallback on a value that is bound to the TextBox's rotation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload