我的24位图像宽320,高240(象素)。
现在我只想取出这个图像的最右边的图片,宽度20个象素。也就是说,我要新生成的图片是20个宽,240个高,是原图像最右边的部分。
以下是我编写的代码:
procedure TForm1.Button4Click(Sender: TObject);
var
  BitMap,bmCentre : TBitMap;
  p1,p2 : PByteArray;
  x,y,nXB: integer;
begin
  BitMap := TBitmap.Create;
  bmCentre := TBitmap.Create;
  try
  begin                     
    BitMap.LoadFromFile('E:\BiowayUrine\image\sss.bmp');
    //
    bmCentre.Width := StrToInt(Edit1.Text);
    bmCentre.Height := BitMap.Height;
    bmCentre.PixelFormat := pf24bit;    nXB := Bitmap.Width - bmCentre.Width;
    
    //CopyMemory(DataPoint,VideoStr^.lpData,Diblen); 
    for y := 0 to bmCentre.Height - 1 do
    begin     
      p1 := BitMap.ScanLine[y];
      p2 := bmCentre.ScanLine[y];      for x := 0 to bmCentre.Width * 3 - 1 do
      begin                     
        p2[x] := p1[x];
      end;                     
    end;                
    bmCentre.SaveToFile('E:\BiowayUrine\image\bottom.bmp');
  end  
  finally
    BitMap.Free;
    bmCentre.Free;
  end;
end;其中edit1是输入的新图像的宽度,也就是说可以是20,也可能是320,看输入。
执行这段代码后,保存的图片不对!!!!特来向大家请教。
第一次做和图片有关的程序,比较迷茫!

解决方案 »

  1.   

    这一段是读图像的全部像素,我只会用c++写:
      Graphics::TBitmap *Bitmap = new Graphics::TBitmap;
      Bitmap->LoadFromFile("earth.bmp");  byte *Pixels = new byte[Bitmap->Width * Bitmap->Height *3 ];  long temp;
      for (long i=0; i<Bitmap->Height; i++) //读像素数据
      {
        for (long j=0; j<Bitmap->Width; j++)
        {
         temp=i* Bitmap->Width * 3+ j * 3;
         Pixels[temp+0] =
                   GetRValue(Bitmap->Canvas->Pixels[j][Bitmap->Height-i-1]);
         Pixels[temp+1] =
                   GetGValue(Bitmap->Canvas->Pixels[j][Bitmap->Height-i-1]);
         Pixels[temp+2] =
                   GetBValue(Bitmap->Canvas->Pixels[j][Bitmap->Height-i-1]);
        }
      }你把循环区域改成你需要的区域。
      

  2.   

    我记得以前看过的帖子,说用Canvas->Pixels不如用ScanLine好,好象速度慢!
    不过我会试一下,谢谢!