gpt4 book ai didi

c# - 如何将位图照片转换为十六进制颜色代码?

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:23 24 4
gpt4 key购买 nike

我正在尝试为我的简历创建一个程序但是我在将位图照片(灰度照片)转换为十六进制或更好地称之为代码时遇到了问题颜色谁能帮帮我?

我尝试用 base64string 转换它,但没有成功。

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();

我本以为会看到一堆数字,例如 A9C255,但我什么也没看到。

最佳答案

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

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();
}
}
}

关于c# - 如何将位图照片转换为十六进制颜色代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55656218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com