动态设置鼠标属性和水印生成器

博客介绍了两项内容,一是动态设置鼠标属性,给出了设计界面及相关代码;二是水印生成器,展示了界面并提供了代码,包含字体、颜色等属性设置及绘制图像的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、动态设置鼠标属性
首先我们先设计好一个界面,如下图:
在这里插入图片描述
代码如下:
[DllImport(“user32.dll”, EntryPoint = “SwapMouseButton”)]
public static extern int SwapMouseButton(int bSwap);

    [DllImport("user32", EntryPoint = "LoadCursorFromFile")]
    public static extern int LoadCursorFromFile(string lpFileName);

    [DllImport("user32", EntryPoint = "SetSystemCursor")]
    public static extern void SetSystemCursor(int hcur, int i);

    private void button6_Click(object sender, EventArgs e)
    {
        if (button6.Text == "鼠标动画效果")
        {
            int cur = LoadCursorFromFile(Application.StartupPath + "\\mouse.ani");
            SetSystemCursor(cur, 32512);
            button6.Text = "恢复为鼠标标准指针";
        }
        else
        {
            int cur = LoadCursorFromFile(@"C:\WINDOWS\Cursors\arrow_m.cur");
            SetSystemCursor(cur, 32512);
            button6.Text = "鼠标动画效果";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Cursor.Hide();    //隐藏鼠标指针
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Cursor.Show();   //显示鼠标指针
    }

    private void button4_Click(object sender, EventArgs e)
    {
        SwapMouseButton(1);    //鼠标左右键交换
    }

    private void button3_Click(object sender, EventArgs e)
    {
        SwapMouseButton(0);    //恢复鼠标默认设置
    }

    private void button5_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start("main.cpl");
}

效果如下图:
在这里插入图片描述

2、水印生成器
界面如下:
在这里插入图片描述
代码如下:

public void PrintSeal()
{
TestOnSeal _top = new TestOnSeal();
_top.TextFont = new Font(“黑体”, 16, FontStyle.Bold);
_top.FillColor = Color.Red;
//_top.ColorTOP = Color.Black;
_top.Text = “www.cnblogs.com/axing”;
_top.BaseString = “康忠鑫专用”;
_top.ShowPath = true;
_top.LetterSpace = 1;
_top.SealSize = 180;
_top.CharDirection = Char_Direction.Center;
_top.SetIndent(20);
// Downloads By http://www.veryhuo.com
Graphics g = this.CreateGraphics();
g.DrawImage(_top.TextOnPathBitmap(), 0, 0);

        _top.CharDirection = Char_Direction.ClockWise;
        g.DrawImage(_top.TextOnPathBitmap(), 180, 0);

        _top.CharDirection = Char_Direction.AntiClockWise;
        g.DrawImage(_top.TextOnPathBitmap(), 0, 180);

        _top.SetIndent(20);
        _top.CharDirection = Char_Direction.Center;
        g.DrawImage(_top.TextOnPathBitmap(), 180, 180);
        pictureBox1.Image = _top.TextOnPathBitmap();
        g.Dispose();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rbtImage.Checked = true;
        PrintSeal();
    }

    /// <summary>
    /// 添加水印,并输出图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnAddWaterMarkAndOutPutImage_Click(object sender, EventArgs e)
    {
        if (pictureBox3.Image == null)
        {
            if (pictureBox2.Image == null)
            {
                MessageBox.Show("请先上传需要添加水印的图片!");
                return;
            }
            string newImage = string.Empty;
            //string Path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string Path = "D:";
            string Path_sy = Path + "\\" + "WaterMarkPath";
            if (Directory.Exists(Path))//判断目录是否存在{}
            {
                Directory.CreateDirectory(Path_sy);
            }
            if (rbtText.Checked)
            {
                AddWater(pictureBox2.Image, Path_sy, out newImage);
            }
            if (rbtImage.Checked)
            {
                AddWaterPic(pictureBox2.Image, Path_sy, pictureBox1.Image, out newImage);
            }
            pictureBox3.Image = Image.FromFile(newImage);

        }
        else
        {
            MessageBox.Show("水印图片已经绑定,请重新选择图片!");
        }
    }

    private void ClearImage()
    {
        pictureBox3.Image = null;
        pictureBox2.Image = null;
    }

    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnUploadImage_Click(object sender, EventArgs e)
    {
        pictureBox3.Image = null;//清空水印图片
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string str = openFileDialog1.FileName;
            int index = str.LastIndexOf("\\");
            suffix = str.Substring(index + 1, str.Length - index - 1);
            pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
        }
    }

    /// <summary>
    /// 在图片上增加文字水印 
    /// </summary>
    /// <param name="pic">pictureBox中的图片</param>
    /// <param name="Path_sy">输出路径</param>
    protected void AddWater(Image pic, string Path_sy, out string newImage)
    {
        newImage = string.Empty;
        try
        {
            string addText = txtContext.Text;
            System.Drawing.Image image = pic; //System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("宋体", 16);    //字体位置为左空10 
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

            g.DrawString(addText, f, b, 14, 14);    //字体大小为14X14 
            g.Dispose();
            index_Text++;
            image.Save(Path_sy + "\\" + index_Text + suffix);
            newImage = Path_sy + "\\" + index_Text + suffix;
            image.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    /// 〈summary> 
    /// 在图片上生成图片水印 
    /// 〈/summary> 
    /// 〈param name="Path">原服务器图片路径〈/param> 
    /// 〈param name="Path_syp">生成的带图片水印的图片路径〈/param> 
    /// 〈param name="Path_sypf">水印图片路径〈/param> 
    protected void AddWaterPic(Image picOld, string Path_syp, Image picWater, out string newImage)
    {
        newImage = string.Empty;
        try
        {
            System.Drawing.Image image = picOld;//System.Drawing.Image.FromFile(Path);
            System.Drawing.Image copyImage = picWater;//System.Drawing.Image.FromFile(Path_sypf);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
            g.Dispose();
            index_Image++;
            image.Save(Path_syp + "\\" + index_Image + suffix);
            newImage = Path_syp + "\\" + index_Image + suffix;
            image.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void rbtImage_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtImage.Checked)
        {
            rbtText.Checked = false;
            txtContext.Visible = false;
        } 
    }

    private void rbtText_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtText.Checked)
        {
            rbtImage.Checked = false;
            txtContext.Visible = true;
        }
}

效果图如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值