取c#背景图在某坐标的颜色

发布网友 发布时间:1天前

我来回答

5个回答

热心网友 时间:20小时前

VS2005 Winform

动态获取当前屏幕中光标所在位置的颜色收藏
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace LiBo.ColorPicker
{

public class Form1 : System.Windows.Forms.Form
{
// 桌面工作区的尺寸
Size workingArea;
// Form 的初始位置和在左下角,右下角的位置
Point formLoc, ptLeftBottom, ptRightBottom;

private System.Windows.Forms.Label lblColor;
private System.Windows.Forms.TextBox txtArgb;
private System.Windows.Forms.Timer tmr;
private System.Windows.Forms.ToolTip tip;
private System.ComponentModel.IContainer components;
[ DllImport ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
int dwRop // 光栅的操作值
);

[ DllImport ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);

private void Form1_DoubleClick(object sender, EventArgs e)
{
formLoc = this.Location;
this.Location = ptRightBottom;
this.TopMost = true;
tmr.Enabled = true;
}

private void tmr_Tick(object sender, EventArgs e)
{
// 创建显示器的DC
IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtr.Zero);
// 从指定设备的句柄创建新的 Graphics 对象
Graphics gfxDisplay = Graphics.FromHdc(hdlDisplay);
// 创建只有一个象素大小的 Bitmap 对象
Bitmap bmp = new Bitmap(1, 1, gfxDisplay);
// 从指定 Image 对象创建新的 Graphics 对象
Graphics gfxBmp = Graphics.FromImage(bmp);
// 获得屏幕的句柄
IntPtr hdlScreen = gfxDisplay.GetHdc();
// 获得位图的句柄
IntPtr hdlBmp = gfxBmp.GetHdc();
// 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中
BitBlt(hdlBmp, 0, 0, 1, 1, hdlScreen, MousePosition.X, MousePosition.Y, 13369376);
// 释放屏幕句柄
gfxDisplay.ReleaseHdc(hdlScreen);
// 释放位图句柄
gfxBmp.ReleaseHdc(hdlBmp);
lblColor.BackColor = bmp.GetPixel(0, 0); // 获取像素的颜色
txtArgb.Text = "0x" + lblColor.BackColor.ToArgb().ToString("x").ToUpper();
gfxDisplay.Dispose();
gfxBmp.Dispose();
bmp.Dispose(); // 释放 bmp 所使用的资源
}
private void txtArgb_KeyPress(object sender, KeyPressEventArgs e)
{
// 当按下ESC键时,确定所取的颜色ARGB值
// 注意:只有当窗体处于激活状态时才有效
if (e.KeyChar == (char)Keys.Escape)
{
tmr.Enabled = false;
this.Location = formLoc;
this.TopMost = false;
txtArgb.SelectAll();
}
}

private void Form1_MouseEnter(object sender, EventArgs e)
{
if (this.Location == ptLeftBottom) //窗体在左下角
{
this.Location = ptRightBottom;
}
else if (this.Location == ptRightBottom) // 窗体在右下角
{
this.Location = ptLeftBottom;
}
}
}
}

热心网友 时间:19小时前

我也试着做了一下..也是好用的.代码这样:

在form的MouseUp事件中:

if(e.Button==System.Windows.Forms.MouseButtons.Left){//如果是鼠标左键
int x=e.X;//取出坐标X
int y=e.Y;//取出坐标Y
Bitmap bm =(Bitmap) this.BackgroundImage;//获取当前背景图
if(bm!=null)//判断有没有背景图
{
Color color= bm.GetPixel(x,y);//获取指定坐标的颜色
MessageBox.Show(color.ToString());//显示
//MessageBox.Show(color.Name);
}
else{
MessageBox.Show("无背景图");
}
}

热心网友 时间:20小时前

改写Form的MouseMove事件,随鼠标指针移动,Form的标题显示颜色信息。

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Bitmap bmp =(Bitmap)this.BackgroundImage;
Color c = bmp.GetPixel(e.X, e.Y);
this.Text = c.ToString();
}

绝对成功,其实关键在于函数Bitmap.GetPixel(int x,int y)就可以在图像上的某一点返回一个颜色Color结构。

热心网友 时间:20小时前

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Bitmap bt = new Bitmap(pictureBox1.Image);
Color cl = new Color();
cl = bt.GetPixel(e.X, e.Y);
label2.Text = cl.ToString();
}

可以取得R G B 三个
拜托给分吧

热心网友 时间:20小时前

Bitmap bitmap = new Bitmap(panel1.Image);
Color myColor = new Color();
myColor = bitmap.GetPixel(1, 1);
myColor.R//
myColor.G//
myColor.B//这是对应的三个RGB色彩参数

--------------------------------------------------

Bitmap bm1 = new Bitmap("D:\\1.jpg");//得到一个bitmap
bm1.GetPixel(x,y);//取到相应坐标的像素的颜色

另:详细信息请参考:http://topic.csdn.net/t/20031024/15/2391184.html

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com