如何将位图照片转换为十六进制颜色代码? [英] How to convert a bitmap photo to Hexadecimal color codes?

查看:129
本文介绍了如何将位图照片转换为十六进制颜色代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的简历创建一个程序,但是将灰度照片的位图照片转换为十六进制或更好地将其称为代码时遇到了问题有颜色可以帮助我的人吗?

I'm trying to create a program for my resume But I have a problem with converting bitmap photo that is greyscale photo to hexadecimal or better calls it the codes if the colors are anyone who can help me?

我试图用 base64string 进行转换,但是没有用.

I tried to convert it with base64string but it didn't work.

openFileDialog1=new OpenFileDialog();
OpenFileDialog dlg = new OpenFileDialog();

dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";

if (dlg.ShowDialog() == DialogResult.OK)
{
     Rectangle bounds = Screen.GetBounds(Point.Empty);
     Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);

using (Graphics g = Graphics.FromImage(bitmap))
{
     g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}

// Convert the image to byte[]
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = stream.ToArray();

// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);

// Write the bytes (as a Base64 string) to the textbox
textBox1.Text = base64String.ToString();

我希望看到一堆数字,例如 A9C 255 ,但是我所看到的没什么.

I expected to see a bunch of numbers like A9C or 255 but the thing that I saw is nothing.

推荐答案

它与您的代码中的.bmp文件一起使用,并提供像素的AARRGGBB代码.

It works with .bmp files as in your code and gives AARRGGBB codes of pixels.

using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title = "Open Image";
                dlg.Filter = "bmp files (*.bmp)|*.bmp";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    var colorCodes = this.GetColorCodes(dlg.FileName);
                    var str = string.Join(Environment.NewLine,
                                          colorCodes.Select<int[], string>(line => string.Join(" ", line.Select<int, string>(code => string.Format("{0:X8}", code))))); // string.Format("{0:X6}", code & 0x00FFFFFF) if you want RRGGBB format
                    textBox1.Text = str; // requires textBox1.Multiline = true, better have monospaced font
                }
            }
        }

        private int[][] GetColorCodes(string path)
        {
            var bitmap = new Bitmap(path);
            return Enumerable.Range(0, bitmap.Height)
                             .Select<int, int[]>(y => Enumerable.Range(0, bitmap.Width)
                                                    .Select<int, int>(x => bitmap.GetPixel(x, y).ToArgb())
                                                    .ToArray())
                             .ToArray();
        }
    }
}

这篇关于如何将位图照片转换为十六进制颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆