Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在WinC窗体中从PictureBox读取像素颜色_C#_Forms_Pixel_Picturebox_Bmp - Fatal编程技术网

C# 尝试在WinC窗体中从PictureBox读取像素颜色

C# 尝试在WinC窗体中从PictureBox读取像素颜色,c#,forms,pixel,picturebox,bmp,C#,Forms,Pixel,Picturebox,Bmp,我有一个奇怪的问题,当我尝试在PictureBox的像素上迭代或保存bmp时,它只是全黑 该工具的目的是选择字体、大小和样式,然后在该字体的ASCII字符上循环,显示图片框中的每个字符,并将像素数据转换为十六进制数组,以便我可以在LCD显示器上使用它们 该工具的主要部分工作原理是正确循环ASCII字符并在图片框中显示它们,但在每个字符被绘制到图片框后,我尝试迭代图片框的像素,每个像素返回为0,0,0 RGB黑色,如果我保存绘制到图片框中的bmp,也会返回全部为黑色,但我仍然可以在PictureB

我有一个奇怪的问题,当我尝试在PictureBox的像素上迭代或保存bmp时,它只是全黑

该工具的目的是选择字体、大小和样式,然后在该字体的ASCII字符上循环,显示图片框中的每个字符,并将像素数据转换为十六进制数组,以便我可以在LCD显示器上使用它们

该工具的主要部分工作原理是正确循环ASCII字符并在图片框中显示它们,但在每个字符被绘制到图片框后,我尝试迭代图片框的像素,每个像素返回为0,0,0 RGB黑色,如果我保存绘制到图片框中的bmp,也会返回全部为黑色,但我仍然可以在PictureBox中看到正确绘制的该字符的bmp,但是PictureBox数据和bmp数据与我在PictureBox中看到的不匹配,我真的不明白为什么我无法正确迭代或保存bmp或PictureBox

我尝试过不使用异步函数,这并不理想,因为我希望UI是免费的,我尝试过各种方法来读取像素和保存bmp,但结果是一样的。我希望问一下,是否有人知道我为什么会有这种奇怪的行为,以及这个问题的解决方案

埃德的问候

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace generateFonts
    {
        public partial class Form1 : Form
        {
            string font = "";
            float fontSize = 0;
            FontStyle fontStyle = FontStyle.Regular;

            public Form1()
            {
                InitializeComponent();
                comboBox1.SelectedIndex = 0;
                comboBox2.SelectedIndex = 0;
                comboBox3.SelectedIndex = 0;

                font = comboBox1.SelectedItem.ToString();
                fontSize = Convert.ToInt32(comboBox2.SelectedItem);
                fontStyle = FontStyle.Regular;
            }

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                font = comboBox1.SelectedItem.ToString();
            }

            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                fontSize = Convert.ToInt32(comboBox2.SelectedItem);
            }

            private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox3.SelectedIndex == 0)
                    fontStyle = FontStyle.Regular;
                else if (comboBox3.SelectedIndex == 1)
                    fontStyle = FontStyle.Italic;
                else if(comboBox3.SelectedIndex == 2)
                    fontStyle = FontStyle.Bold;
            }

            private async void button1_Click(object sender, EventArgs e)
            {
                await Task.Run(() => StartProcess(1));
            }

            private void StartProcess(int runs)
            {
                // Font 
                Font myFont = new Font(font, fontSize, fontStyle);

                List<string> bytes = new List<string>();

                string[] bits = { "0", "0", "0", "0", "0", "0", "0", "0" };
                byte bitPos = 0;

                for (byte i = 32; i < 126; i++)
                {
                    //Create a Image-Object on which we can paint
                    Image bmp = new Bitmap(200, 200);

                    //Create the Graphics-Object to paint on the Bitmap
                    Graphics g = Graphics.FromImage(bmp);

                    string c = Convert.ToChar(i).ToString();

                    //Get the perfect Image-Size so that Image-Size = String-Size
                    SizeF size = g.MeasureString(c, myFont);

                    //Use this to become better Text-Quality on Bitmap.
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;

                    //Here we draw the string on the Bitmap
                    g.DrawString(c, myFont, new SolidBrush(Color.Black), 0, 0);

                    if (!Directory.Exists("FontBmps"))
                        Directory.CreateDirectory("FontBmps");

                    this.Invoke((MethodInvoker)delegate ()
                    {
                        pictureBox2.Width = Convert.ToInt32(size.Width);
                        pictureBox2.Height = Convert.ToInt32(size.Height);
                        pictureBox2.Image = bmp; // <--- this is working and the picturebox shows the bmp correctly
                        bmp.Save("FontBmps/" + i + "_" + font + "_" + fontSize + "px_" + fontStyle + ".bmp", ImageFormat.Bmp); // <--- error here: this saves a black square instead of the bmp i see displayed in the picturebox GUI ??
                        // Even if i save the picturebox itself that too is just a black square instead of the correct image shown in the GUI ??

                        // now convert the bmp to a HEX array of pixels
                        for (int h = 0; h < pictureBox2.Height; h++)
                        {
                            for (int w = 0; w < pictureBox2.Width; w++)
                            {
                                Color colour = (pictureBox2.Image as Bitmap).GetPixel(w, h);

                                if (colour.R == 0 && colour.G == 0 && colour.B == 0)
                                {
                                    bits[bitPos] = "1";
                                }
                                else
                                {
                                    bits[bitPos] = "0"; // <-- never hits me, again for some reason the bmp or picturebox is all black pixels data but i see it correctly show in the picturebox GUI ??
                                }

                                if (bitPos < 7)
                                    bitPos++;
                                else
                                {
                                    //string bitStr = bits.ToString();
                                    //string hex = Convert.ToByte(bitStr, 2).ToString("x2");
                                    //bytes.Add(hex);
                                    bitPos = 0;
                                    for(byte n = 0; n < 8; n++)
                                    {
                                        bits[n] = "0";
                                    }
                                }

                            }
                        }

                        if (bitPos != 0)
                        {
                            // TO DO...
                        }

                    });
                    Thread.Sleep(500); // <--- i have this just to see it displaying the chars but i have removed it with no effect to the issue 
                }
                // now add List to a file in the correct format
                foreach (string str in bytes)
                {
                    Console.WriteLine(str);
                    // TO DO...
                }
            }


        }
    }

我相信图像是黑色的,但有些部分是透明的。也就是说,您需要检查Alpha。您在图片框中看到的是透明图片框的背景色

鉴于ImageFormat.Bmp不支持透明度,您将无法在保存的文件中看到透明度。请尝试Png,它支持透明性并具有无损压缩

或者,在绘制之前,您可以使用将图像设置为您想要的背景白色的颜色

除此之外,我建议使用bmp而不是pictureBox2.Image作为位图,并使用。这将提高性能

这可能有助于参考:。另见