gpt4 book ai didi

c# - 为什么在 Visual C# 中将像素写入图片框时遇到问题?

转载 作者:行者123 更新时间:2023-12-03 23:33:21 24 4
gpt4 key购买 nike

我正在编写一个模拟器程序,虚拟显示器应该能够接收 3 个字节的颜色数据并显示正确的颜色像素,类似于真实屏幕的工作方式。但是当我设置一些滚动条来测试像素的生成时,什么也没有发生。这是我的代码和表单的屏幕截图:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;




namespace TSC_Multi_System_Emulator
{

public partial class Form1 : Form
{
private PictureBox Display = new PictureBox();
string @emulationfolderpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Bitmap screen = new Bitmap(@Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Resource_Folder\" + @"FirstFrame.bmp");
int x = 0;
int y = 0;

public Form1()
{
InitializeComponent();

}
private void Form1_Load(object sender, System.EventArgs e) {
// Dock the PictureBox to the form and set its background to black.
Display.BackColor = Color.Black;
// Connect the Paint event of the PictureBox to the event handler method.

// Add the PictureBox control to the Form.
this.Controls.Add(Display);


}
public void DigitalGraphicsDisplay(int red, int green, int blue) {
Graphics g = Display.CreateGraphics();
screen.SetPixel(x, y, Color.FromArgb(red, green, blue));
g.DrawImage(screen, 0, 0, screen.Width, screen.Height);
g.Save();
if (x < screen.Width)
{
x = x + 1;
}
else if (x == screen.Width)
{
x = 0;
if (y < screen.Height)
{
y = y + 1;
}
else if (y == screen.Height)
{
y = 0;
}
}
}



private void button1_Click(object sender, EventArgs e){
int rchannel = redControl.Value;
int gchannel = greenControl.Value;
int bchannel = blueControl.Value;
DigitalGraphicsDisplay(rchannel, gchannel, bchannel);
}




}
}

Screenshot of IDE

更新:

代码现在可以正常工作,但我无法仅使用测试按钮来测试代码。我必须使用第一个答案中给我的确切代码,它只显示渐变,我想知道我做错了什么......:(

public partial class Form1 : Form
{

string @emulationfolderpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Bitmap screen = new Bitmap(@Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Resource_Folder\" + @"FirstFrame.bmp");
int x = 0;
int y = 0;

public Form1()
{
InitializeComponent();

}
private void Form1_Load(object sender, System.EventArgs e) {
// Dock the PictureBox to the form and set its background to black.
Display.BackColor = Color.Black;
// Connect the Paint event of the PictureBox to the event handler method.

// Add the PictureBox control to the Form.
this.Controls.Add(Display);


}

public void DigitalGraphicsDisplay(int red, int green, int blue)
{
if (Display.Image == null)
{
Bitmap NewBMP = new Bitmap(Display.ClientRectangle.Width, Display.ClientRectangle.Height);
using (Graphics g = Graphics.FromImage(NewBMP))
{
g.Clear(Color.White);
}
Display.Image = NewBMP;
}

(Display.Image as Bitmap).SetPixel(x, y, Color.FromArgb(red, green, blue));

Display.Invalidate();

x++;

if (x >= Display.Image.Width)
{
x = 0;
y++;

if (y >= Display.Image.Height)
{
y = 0;
}
}
}


private void button1_Click(object sender, EventArgs e){
Boolean a = false;
int b = 0;
do
{
DigitalGraphicsDisplay(51, 153, 102);
if (b == 10000)
{
a = true;
}
b = b + 1;
} while (a);

}





}

}

我得到的只是一个白色的图片盒,里面没有其他东西......(渐变代码确实有效)

最佳答案

看起来您正在尝试直接在 PictureBox 控件本身上绘图。

相反,您应该将图像分配给 PictureBox,然后在该图像上绘图。

尝试更改您的代码,如下所示。 (包括测试用的点击事件。)
请注意,PictureBox 直接保留对图像的引用,因此您不需要在类中使用单独的屏幕图像,除非您有不同的目的。

此外,这还使用了Bitmap.SetPixel(),这是一种非常慢的设置像素的方法。在这些其他链接中,有一种更快但稍微复杂的方法:

请记住,单击按钮一次只会绘制一个像素。
所以一定要仔细看:
enter image description here

在点击事件中运行我的测试代码将产生以下结果:
enter image description here

int x = 0;
int y = 0;
public void DigitalGraphicsDisplay(int red, int green, int blue)
{
if (Display.Image == null)
{
Bitmap NewBMP = new Bitmap(Display.ClientRectangle.Width, Display.ClientRectangle.Height);
using (Graphics g = Graphics.FromImage(NewBMP))
{
g.Clear(Color.White);
}
Display.Image = NewBMP;
}

(Display.Image as Bitmap).SetPixel(x, y, Color.FromArgb(red, green, blue));

Display.Invalidate();

x++;

if (x >= Display.Image.Width)
{
x = 0;
y++;

if (y >= Display.Image.Height)
{
y = 0;
}
}
}

private void button1_Click(object sender, EventArgs e)
{
// Temporary code to show that it works (Due to Bitmap.SetPixel() it will be slow)
for (int I = 1; I < Display.ClientRectangle.Width * Display.ClientRectangle.Height; I++)
DigitalGraphicsDisplay((I/255)%255, (I % Display.ClientRectangle.Width) % 255, 127);
}

更新:根据您的评论,请尝试以下示例代码:

private void button1_Click(object sender, EventArgs e)
{
Boolean a = true;
int b = 0;
do
{
DigitalGraphicsDisplay(51, 153, 102);

if (b == 10000)
{
a = false;
}

b = b + 1;
} while (a);
}

关于c# - 为什么在 Visual C# 中将像素写入图片框时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34389081/

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