Results 1 to 2 of 2

Thread: [RESOLVED] Using PNG image as cursor in WPF

  1. #1

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    Resolved [RESOLVED] Using PNG image as cursor in WPF

    1- Create a Winform / vb.net project.

    2- Put a png file on your desktop and name it as myimage.png

    3- The following code changes the cursor.

    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            'Fisrt convert your image to bmp format
            Dim myImage As Image = Image.FromFile("C:\Users\Yourname\Desktop\myimage.png")
            Using myMemoryStream As New IO.MemoryStream()
                myImage.Save(myMemoryStream, Imaging.ImageFormat.Bmp)
            End Using
            Dim myPictureBox As New PictureBox
            myPictureBox.Image = myImage
            Me.Cursor = New Cursor(DirectCast(myPictureBox.Image, Bitmap).GetHicon())
        End Sub
    End Class
    My question is here:

    I want the same action in WPF.

    So do you know WPF version of the above code?



    This is C# version of the above code.

    Code:
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Image myImage = Image.FromFile("C:\\Users\\Yourname\\Desktop\\myimage.png");
            using (System.IO.MemoryStream myMemoryStream = new System.IO.MemoryStream())
            {
                myImage.Save(myMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
            }
            PictureBox myPictureBox = new PictureBox();
            myPictureBox.Image = myImage;
            this.Cursor = new Cursor(((Bitmap)myPictureBox.Image).GetHicon());
        }
    }
    }
    Last edited by Kram Kramer; May 18th, 2020 at 08:34 PM.

  2. #2

    Re: Using PNG image as cursor in WPF

    Hii Kram,

    I got some sites to create cursor using png image.

    Please check the below links...
    https://web.archive.org/web/20150504...custom-cursors
    https://wpf.2000things.com/2012/12/1...hile-dragging/

    I hope it will help you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width