zoukankan      html  css  js  c++  java
  • 图片取色小程序

    本小程序功能:在图片上取某一点的颜色
    主要代码如下:
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using bitmapTest.Properties;

    namespace bitmapTest
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }

            Bitmap myBitmap;
            
    int cmd = 0;

            
    private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.Image 
    = Resources.bfq0002;
                myBitmap 
    = (Bitmap)pictureBox1.Image;
            }
            
    /// <summary>
            
    /// 鼠标取色(移动)
            
    /// </summary>
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                
    int mouseX = e.X;
                
    int mouseY = e.Y;
                txtPoint.Text 
    = "(" + mouseX + "," + mouseY + ")";
                
    if (rbtmutil.Checked)
                {
                    
    //取色
                    try
                    {
                        Color pixelColor 
    = myBitmap.GetPixel(mouseX, mouseY);
                        colorbox.BackColor 
    = pixelColor;
                        txtColorValue.Text 
    = System.Drawing.ColorTranslator.ToHtml(pixelColor);
                    }
                    
    catch
                    { }
                }

                
    //命令
                switch (cmd)
                {
                    
    case 1:  //画线
                        SolidBrush pixelBrush = new SolidBrush(Color.Red);
                        Graphics g 
    = Graphics.FromImage(myBitmap);
                        g.FillRectangle(pixelBrush, mouseX, mouseY, 
    22);
                        pictureBox1.Image 
    = myBitmap;
                        
    break;
                    
    case 2:
                        
    break;
                    
    default:
                        
    break;
                }
            }
            
    /// <summary>
            
    /// 画笔
            
    /// </summary>
            private void btnpen1_Click(object sender, EventArgs e)
            {
                
    if (cmd == 1)
                {
                    cmd 
    = 0;
                    btnpen1.Image 
    = Resources.png_05252;
                    
    this.toolTip1.SetToolTip(this.btnpen1, "单击使用画笔");
                }
                
    else
                {
                    cmd 
    = 1;
                    btnpen1.Image 
    = Resources.png_05253;
                    
    this.toolTip1.SetToolTip(this.btnpen1, "单击禁用画笔");
                }
            }
            
    /// <summary>
            
    /// 恢复
            
    /// </summary>
            private void button1_Click(object sender, EventArgs e)
            {
                pictureBox1.Image 
    = Resources.bfq0002;
                myBitmap 
    = (Bitmap)pictureBox1.Image;
            }
            
    /// <summary>
            
    /// 鼠标取色(单击)
            
    /// </summary>
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                
    //取色
                try
                {
                    Color pixelColor 
    = myBitmap.GetPixel(e.X, e.Y);
                    colorbox.BackColor 
    = pixelColor;
                    txtColorValue.Text 
    = System.Drawing.ColorTranslator.ToHtml(pixelColor);
                }
                
    catch
                { }
            }

        }
    }

    程序界面如下:


    演示版下载: Demo         程序源码:Code
  • 相关阅读:
    Systemd 入门教程:实战篇
    Systemd 入门教程:命令篇
    awk详解
    yum Multilib version problems
    Oracle ORA 12541 报错解决过程
    创建表空间
    大文件按行分离脚本
    heartbeat安装与配置
    Keepalived安装配置
    速查表的名称和建表语句
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1094715.html
Copyright © 2011-2022 走看看