Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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#将原始像素数据绘制到图片框问题_C#_Winforms_Delphi_Bitmap - Fatal编程技术网

帕斯卡>;C#将原始像素数据绘制到图片框问题

帕斯卡>;C#将原始像素数据绘制到图片框问题,c#,winforms,delphi,bitmap,C#,Winforms,Delphi,Bitmap,在Delphi7中,我可以使用图像组件来实现这一点,这一切都很好,但我正在尝试将相同的功能转换为C#,我遇到了一些问题 procedure TMain_Form.Load_Pallete_WPN(FileName: string); var MOMO_Fs: TFileStream; i, ii, iii: integer; begin MOMO_Fs := TFileStream.Create(FileName, fmOpenRead); MOMO_Fs.Position :=

在Delphi7中,我可以使用图像组件来实现这一点,这一切都很好,但我正在尝试将相同的功能转换为C#,我遇到了一些问题

procedure TMain_Form.Load_Pallete_WPN(FileName: string);
var
  MOMO_Fs: TFileStream;
  i, ii, iii: integer;
begin
  MOMO_Fs := TFileStream.Create(FileName, fmOpenRead);
  MOMO_Fs.Position := $82D80;
  MOMO_Fs.Read(RGBA, $400);

  iii := 0;
  for i := 0 to 15 do
  begin
    for ii := 0 to 15 do
    begin
      Pallet_Preview.Picture.Bitmap.Canvas.Pixels[ii, i] :=
        RGB(RGBA[iii].b, RGBA[iii].G, RGBA[iii].R);
      inc(iii);
    end;
  end;

  MOMO_Fs.Free;
end;

这是pascal中的工作结果,只是为了澄清一下。 现在,当我尝试在C#中实现这种行为时,我尝试了以下方法

public struct TRGBA_Obj
{

    public byte b;
    public byte g;
    public byte r;
    public byte a;

}


       public TRGBA_Obj[] RGBA = new TRGBA_Obj[255];
       public string fp;


    public void Load_Pallette(string filepath)
    {
        int i, ii, iii;

        FileStream fs = new FileStream(filepath, FileMode.Open);
        BinaryReader br = new BinaryReader(fs);



            Bitmap bmp = new Bitmap(256, 256);



        fs.Seek(0x82D80, SeekOrigin.Begin); // this is where the palette is  


        for (int x = 0; x < RGBA.Length; x++)
        {
            RGBA[x].r = br.ReadByte();
            RGBA[x].g = br.ReadByte();
            RGBA[x].b = br.ReadByte();
            RGBA[x].a = br.ReadByte();


               // this just dumps the color values to a list view for ref
            LV_COLOR_PALETTE.Items.Add(x.ToString());
            LV_COLOR_PALETTE.Items[x].SubItems.Add(RGBA[x].r.ToString());
            LV_COLOR_PALETTE.Items[x].SubItems.Add(RGBA[x].g.ToString());
            LV_COLOR_PALETTE.Items[x].SubItems.Add(RGBA[x].b.ToString());
            LV_COLOR_PALETTE.Items[x].SubItems.Add(RGBA[x].a.ToString());

        }

        iii = 0;

        for (int y = 0; y < 15; y++)
        {
            for (int j = 0; j < 15; j++)
            {
               bmp.SetPixel(j, y, Color.FromArgb(RGBA[iii].a, RGBA[iii].r, RGBA[iii].g, RGBA[iii].b));

                iii++;   
            }
        }



        random_box.Image = bmp;


        fs.Close();
        br.Close();
public struct TRGBA_Obj
{
公共字节b;
公共字节g;
公共字节r;
公共字节a;
}
公共TRGBA_Obj[]RGBA=新TRGBA_Obj[255];
公共字符串fp;
公共无效加载托盘(字符串文件路径)
{
int i、ii、iii;
FileStream fs=newfilestream(filepath,FileMode.Open);
BinaryReader br=新的BinaryReader(fs);
位图bmp=新位图(256,256);
Seek(0x82D80,SeekOrigin.Begin);//这是调色板所在的位置
对于(int x=0;x

我对pascal示例的结果符合预期,因为它只是一个256像素的调色板。C#绘制的图像非常小。我尝试了picturebox的所有缩放/拉伸选项,如果我将bmp的高度和宽度从256/256更改为较低的值,图像大小会增加,但看起来与delphi示例完全不同,有什么提示吗?哈哈

您显示的pascal代码不会生成您显示的图像。图像可能是256 x 256像素,但您最多只能寻址15 x 15像素。如果您注意正确设置代码格式,则很容易发现,因此我为您设置了格式

您的托盘有256种颜色,很明显,您显示的图像将每种颜色显示为16 x 16像素块。您需要再添加两个循环来处理这些块

然后,要解决图像中的像素问题,需要计算水平和垂直方向的
block*16+pixwithinblock

pascal代码可能如下所示:

procedure TForm6.Button2Click(Sender: TObject);
var
  x, y: integer; // block indexes
  i, ii, // pixels within a block
  iii: integer; // palette index
begin
  iii := 0;
  for y := 0 to 15 do
  begin
    for x := 0 to 15 do
    begin
      for i := 0 to 15 do
      begin
        for ii := 0 to 15 do
        begin
          Image1.Canvas.Pixels[x * 16 + ii, y * 16 + i] :=
            RGB(RGBA[iii].rgbBlue, RGBA[iii].rgbGreen, RGBA[iii].rgbRed);
        end;
      end;
      inc(iii);
    end;
  end;
end;
请注意,当块索引更改时,颜色索引也会更改

或者,您可以在水平和垂直方向将像素寻址为0..255,并按如下方式计算颜色索引:

procedure TForm6.Button3Click(Sender: TObject);
var
  i, ii, iii: integer;
begin
  for i := 0 to 255 do
    for ii := 0 to 255 do
    begin
      iii := (i div 16) * 16 + (ii div 16);
      Image1.Canvas.Pixels[ii, i] :=
        RGB(RGBA[iii].rgbBlue, RGBA[iii].rgbGreen, RGBA[iii].rgbRed);
    end;
end;
我不是一名C#程序员,但似乎您已经正确地进行了转换,因此我相信您也可以转换这个修改过的pascal代码