当前位置: 首页 > news >正文

便签贴文字生成

目录

效果 

代码 

下载 


效果 

代码 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;

namespace Note
{
    public partial class frmMain : Form
    {

        string info = "";
        string ImgPath = Application.StartupPath + "\\Img\\";
        string TplPath = Application.StartupPath + "\\NoteTpl\\";

        FontDialog fontDialog1 = new FontDialog();
        ColorDialog colorDialog1 = new ColorDialog();
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            info = txtInfo.Text;
            lblInfo.Text = info;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(ImgPath) == false)
            {
                Directory.CreateDirectory(ImgPath);
            }
            string path = ImgPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

            Bitmap bitmap = GetPicThumbnail(picInfo.Image, picInfo.Height, picInfo.Width, 100);
            Graphics g = Graphics.FromImage(bitmap);

            Font font = lblInfo.Font;
            Brush brush = new SolidBrush(lblInfo.ForeColor);
            PointF point = lblInfo.Location;
            g.DrawString(info, font, brush, point);

            bitmap.Save(path);

            System.Diagnostics.Process.Start("explorer.exe", ImgPath);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            lblInfo.Parent = picInfo;
            lblInfo.BackColor = Color.Transparent;
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            //显示字体对话框
            DialogResult dr = fontDialog1.ShowDialog();
            //如果在对话框中单击“确认”按钮,则更改文本框中的字体
            if (dr == DialogResult.OK)
            {
                lblInfo.Font = fontDialog1.Font;
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                lblInfo.ForeColor = colorDialog1.Color;
            }
        }

        private void btnTplSelect_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = TplPath;//设置打开路径的目录
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                picInfo.Image = new Bitmap(openFileDialog1.FileName);
            }
        }

        #region lbl拖动

        [DllImport("User32.DLL")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("User32.DLL")]
        public static extern bool ReleaseCapture();
        public const uint WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 61456;
        public const int HTCAPTION = 2;

        private void lblInfo_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(((Label)sender).Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
        }

        #endregion

        #region 无损压缩图片

        public static Bitmap GetPicThumbnail(System.Drawing.Image iSource, int dHeight, int dWidth, int flag)
        {
            ImageFormat tFormat = iSource.RawFormat;
            int sW = 0, sH = 0;

            //按比例缩放
            Size tem_size = new Size(iSource.Width, iSource.Height);

            if (tem_size.Width > dHeight || tem_size.Width > dWidth)
            {
                if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
                {
                    sW = dWidth;
                    sH = (dWidth * tem_size.Height) / tem_size.Width;
                }
                else
                {
                    sH = dHeight;
                    sW = (tem_size.Width * dHeight) / tem_size.Height;
                }
            }
            else
            {
                sW = tem_size.Width;
                sH = tem_size.Height;
            }

            Bitmap ob = new Bitmap(dWidth, dHeight);
            Graphics g = Graphics.FromImage(ob);

            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);

            g.Dispose();
            //设置压缩质量  
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-100  
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                return ob;
            }
            catch
            {
                return null;
            }

        }

        #endregion

    }
}
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;namespace Note
{public partial class frmMain : Form{string info = "";string ImgPath = Application.StartupPath + "\\Img\\";string TplPath = Application.StartupPath + "\\NoteTpl\\";FontDialog fontDialog1 = new FontDialog();ColorDialog colorDialog1 = new ColorDialog();OpenFileDialog openFileDialog1 = new OpenFileDialog();public frmMain(){InitializeComponent();}private void btnCreate_Click(object sender, EventArgs e){info = txtInfo.Text;lblInfo.Text = info;}private void btnSave_Click(object sender, EventArgs e){if (Directory.Exists(ImgPath) == false){Directory.CreateDirectory(ImgPath);}string path = ImgPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";Bitmap bitmap = GetPicThumbnail(picInfo.Image, picInfo.Height, picInfo.Width, 100);Graphics g = Graphics.FromImage(bitmap);Font font = lblInfo.Font;Brush brush = new SolidBrush(lblInfo.ForeColor);PointF point = lblInfo.Location;g.DrawString(info, font, brush, point);bitmap.Save(path);System.Diagnostics.Process.Start("explorer.exe", ImgPath);}private void frmMain_Load(object sender, EventArgs e){lblInfo.Parent = picInfo;lblInfo.BackColor = Color.Transparent;}private void btnFont_Click(object sender, EventArgs e){//显示字体对话框DialogResult dr = fontDialog1.ShowDialog();//如果在对话框中单击“确认”按钮,则更改文本框中的字体if (dr == DialogResult.OK){lblInfo.Font = fontDialog1.Font;}}private void btnColor_Click(object sender, EventArgs e){if (colorDialog1.ShowDialog() == DialogResult.OK){lblInfo.ForeColor = colorDialog1.Color;}}private void btnTplSelect_Click(object sender, EventArgs e){openFileDialog1.InitialDirectory = TplPath;//设置打开路径的目录if (openFileDialog1.ShowDialog() == DialogResult.OK){picInfo.Image = new Bitmap(openFileDialog1.FileName);}}#region lbl拖动[DllImport("User32.DLL")]public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);[DllImport("User32.DLL")]public static extern bool ReleaseCapture();public const uint WM_SYSCOMMAND = 0x0112;public const int SC_MOVE = 61456;public const int HTCAPTION = 2;private void lblInfo_MouseDown(object sender, MouseEventArgs e){ReleaseCapture();SendMessage(((Label)sender).Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);}#endregion#region 无损压缩图片public static Bitmap GetPicThumbnail(System.Drawing.Image iSource, int dHeight, int dWidth, int flag){ImageFormat tFormat = iSource.RawFormat;int sW = 0, sH = 0;//按比例缩放Size tem_size = new Size(iSource.Width, iSource.Height);if (tem_size.Width > dHeight || tem_size.Width > dWidth){if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth)){sW = dWidth;sH = (dWidth * tem_size.Height) / tem_size.Width;}else{sH = dHeight;sW = (tem_size.Width * dHeight) / tem_size.Height;}}else{sW = tem_size.Width;sH = tem_size.Height;}Bitmap ob = new Bitmap(dWidth, dHeight);Graphics g = Graphics.FromImage(ob);g.Clear(Color.WhiteSmoke);g.CompositingQuality = CompositingQuality.HighQuality;g.SmoothingMode = SmoothingMode.HighQuality;g.InterpolationMode = InterpolationMode.HighQualityBicubic;g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);g.Dispose();//设置压缩质量  EncoderParameters ep = new EncoderParameters();long[] qy = new long[1];qy[0] = flag;//设置压缩的比例1-100  EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);ep.Param[0] = eParam;try{ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();ImageCodecInfo jpegICIinfo = null;for (int x = 0; x < arrayICI.Length; x++){if (arrayICI[x].FormatDescription.Equals("JPEG")){jpegICIinfo = arrayICI[x];break;}}return ob;}catch{return null;}}#endregion}
}

下载 

源码下载


http://www.taodudu.cc/news/show-8456990.html

相关文章:

  • 能被15整除的最大整数
  • WSL2 配置yolov5_caffe
  • 【C/C++】2.9 C语言基础
  • 9.3 程序的控制结构
  • jieba切词的函数区别
  • python lcut()函数_Python3数据结构
  • python打开一个不存在的文件报错_当打开一个不存在的文件时,以下选项中描述正确的是...
  • Android服务器的通信方式
  • 重重难关终于做到了跑满10g带宽?
  • ES应用_Lucene知识
  • ElasticSearch那些事儿(九)
  • 代码注释之程序猿天真可爱无国界!
  • 一文看懂TCP/IP中的相关知识
  • 开发数据产品+AI产品通关上岸课程
  • AI产品经理-借力
  • 七种令人惊叹的人工智能工具
  • cin.ignore()
  • delete 与 delete[ ] 区别
  • C++信号处理 [ signal()函数 raise()函数 ]
  • C++存储类
  • 第13章 宣泄的拥抱
  • 【附源码】Java计算机毕业设计喜枫日料店自助点餐系统(程序+LW+部署)
  • Java实现餐厅自助点餐系统【附源码】
  • 微信小程序java高校食堂窗口自助点餐系统uniapp
  • 基于微信小程序的食堂窗口自助点餐系统设计与实现-计算机毕业设计源码+LW文档
  • 自助点餐系统(三)
  • JAVA实现自助点餐系统【附源码】
  • 【Vue H5项目实战】从0到1的自助点餐系统—— 项目页面布局(Vue3.2 + Vite + TS + Vant + Pinia + Nodejs + MongoDB)
  • P5706 【深基2.例8】再分肥宅水 C语言
  • 再分肥宅水