请问如何水平翻转Bitmap?

yancey 2004-12-29 02:08:19
var b:=TBitmap.Create();
b.assign(jpeg);

请问,如何将b水平翻转?
...全文
373 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hexenzhou 2005-09-09
  • 打赏
  • 举报
回复
CopyRect这个方法最简单了,还真没有想到,学习!
luxuinstan 2005-09-07
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
bmp1,bmp2:TBitmap;
SrcR,DesR:TRect;
x,y:Integer;
begin
bmp1:=TBitmap.Create;
bmp2:=Tbitmap.Create;
Try
bmp1.LoadFromFile('e:\test.bmp');
x:=bmp1.Width;
y:=bmp1.Height;
SrcR:=Rect(0,0,x,y);
DesR:=Rect(x,0,0,y);
bmp2.Width:=x;
bmp2.Height:=y;
bmp2.Canvas.CopyRect(DesR,bmp1.Canvas,SrcR);
canvas.Draw(0,0,bmp2); //将反转后的图像画在窗体上
Finally
bmp1.Free;
bmp2.Free;
end;
end;

这个方法正确
newlife2005 2005-01-06
  • 打赏
  • 举报
回复
学习
constantine 2005-01-05
  • 打赏
  • 举报
回复
用copyrect直接吧rect参数坐标调一下就可以了
todouwang 2005-01-05
  • 打赏
  • 举报
回复
mark;
eWong2016 2005-01-04
  • 打赏
  • 举报
回复
学习:)
ehom 2005-01-04
  • 打赏
  • 举报
回复
type
PBGRA = ^TBGRA;
TBGRA = packed record
B, G, R, A: Byte;
end;

procedure FlipHorz(const ABitmap: TBitmap);
var
i, j: Integer;
SrcRow, DestRow, Row: PBGRA;
Width, Height: Integer;
begin
//for pf32bit;
Width := ABitmap.Width;
Height := ABitmap.Height;
GetMem(Row, 4);
for J := 0 to Height - 1 do
begin
SrcRow := ABitmap.ScanLine[j];
DestRow := SrcRow;
Inc(DestRow, Width - 1);
for I := 0 to (Width div 2) - 1 do
begin
Row^ := SrcRow^;
SrcRow^ := DestRow^;
DestRow^ := Row^;
Inc(SrcRow);
Dec(DestRow);
end;
end;
FreeMem(Row);
end;
blfriend_2000 2005-01-04
  • 打赏
  • 举报
回复
其实我觉得只要把握好原理就好。

其实原理很简单的就是把水平的象素点的RGB值对称的换掉就行

比如象素点 1 2 3 4 5 6……n
1 2 3 4 5 6……n
……
……

只要把每一行的 1<->n , 2<->n-1 , 3<->n-2 ……象素点的RGB值互换就行。

如果垂直反转的话就互换列对应的象素点的RGB值。
jackie168 2005-01-04
  • 打赏
  • 举报
回复
//水平翻转:

procedure FlipHorz(const Bitmap:TBitmap);

var

i,j:Integer;

rowIn,rowOut:pRGBTriple;

Bmp:TBitmap;

Width,Height:Integer;

begin

Bmp:=TBitmap.Create;

Bmp.Width := Bitmap.Width;

Bmp.Height := Bitmap.Height;

Bmp.PixelFormat := pf24bit;

Width:=Bitmap.Width-1;

Height:=Bitmap.Height-1;

for j := 0 to Height do

begin

rowIn := Bitmap.ScanLine[j];

for i := 0 to Width do

begin

rowOut := Bmp.ScanLine[j];

Inc(rowOut,Width - i);

rowOut^ := rowIn^;

Inc(rowIn);

end;

end;

Bitmap.Assign(Bmp);

end;

alexanda2000 2005-01-04
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
bmp1,bmp2:TBitmap;
SrcR,DesR:TRect;
x,y:Integer;
begin
bmp1:=TBitmap.Create;
bmp2:=Tbitmap.Create;
Try
bmp1.LoadFromFile('e:\test.bmp');
x:=bmp1.Width;
y:=bmp1.Height;
SrcR:=Rect(0,0,x,y);
DesR:=Rect(x,0,0,y);
bmp2.Width:=x;
bmp2.Height:=y;
bmp2.Canvas.CopyRect(DesR,bmp1.Canvas,SrcR);
canvas.Draw(0,0,bmp2); //将反转后的图像画在窗体上
Finally
bmp1.Free;
bmp2.Free;
end;
end;
Kshape 2005-01-01
  • 打赏
  • 举报
回复
关注ing...........
caiso 2004-12-30
  • 打赏
  • 举报
回复
哥们,都来关注一下,
关注ing...........
火猴 2004-12-30
  • 打赏
  • 举报
回复
楼上的够长~~
XuDunYu 2004-12-29
  • 打赏
  • 举报
回复
study
xiaoxiao197821 2004-12-29
  • 打赏
  • 举报
回复
procedure RotateBmp(bmp: TBitmap; Center: TPoint; angle: Integer);
var
tmpbmp: TBitmap;
i, j, x, y, px, py: Integer;
cAngle, sAngle: extended;
p1, p2: Pchar;
begin
while angle < 0 do
angle := angle + 360;
angle := angle mod 360;
sAngle := sin(- angle * pi / 180);
cAngle := cos(- angle * pi / 180);
tmpbmp := tbitmap.create;
tmpbmp.assign(bmp);
for i := 0 to tmpbmp.height - 1 do
begin
p1 := pchar(tmpbmp.scanline[i]);
py := 2 * (i - center.y) - 1;
for j := 0 to tmpbmp.width - 1 do
begin
px := 2 * (j - center.x) - 1;
x := (round(px * cAngle - py * sAngle) - 1) div 2 + center.x;
y := (round(px * sAngle + py * cAngle) - 1) div 2 + center.y;
if (x>=0) and (x<tmpbmp.width) and (y>=0) and (y<=tmpbmp.height) then
begin
p2 := pchar(bmp.scanline[y]) + x * 3;
move(p1^, p2^, 3);
end;
inc(p1, 3);
end;
end;
end;
qiujsh 2004-12-29
  • 打赏
  • 举报
回复
Here's code to rotate a bitmap 90 degrees

From: Dave Shapiro <daves@cyber-fx.com>
Counterclockwise, that is. This rotates a 640x480 24-bit bitmap 90 degrees in about 2/10 of sec on my P133 (4MB video card, 128MB RAM). It works for Delphi 2, and should work for 1 and 3, too.

One thing: This does not work for bitmaps that aren't an integral number of colors per pixel. If that's the case, you'll have to do some bit twiddling. I'm working on that part right now (I need it too), but it'll be a few days. Anyways, suggestions, comments, etc., always welcome.

Special thanks to David Ullrich (ooh, I hope I spelled that correctly) for pointing out the non-published-but-public-anyway SaveToStream method for the TBitmap class.

Advance apologies for any formatting problems. Netscape's editor is so ridiculously crippled that I can't accomplish anything (give me rtin and vim any day).



--------------------------------------------------------------------------------

procedure RotateBitmap90Degrees(ABitmap: TBitmap);

const
BITMAP_HEADER_SIZE = SizeOf(TBitmapFileHeader) +
SizeOf(TBitmapInfoHeader);

var
{ Things that end in R are for the rotated image. }
PbmpInfoR: PBitmapInfoHeader;
bmpBuffer, bmpBufferR: PByte;
MemoryStream, MemoryStreamR: TMemoryStream;
PbmpBuffer, PbmpBufferR: PByte;
PbmpBufferRFirstScanLine, PbmpBufferColumnZero: PByte;
BytesPerPixel, BytesPerScanLine, BytesPerScanLineR: Integer;
X, Y, T: Integer;

begin
{
Don't *ever* call GetDIBSizes! It screws up your bitmap.
I'll be posting about that shortly.
}

MemoryStream := TMemoryStream.Create;

{
To do: Put in a SetSize, which will eliminate any reallocation
overhead.
}

ABitmap.SaveToStream(MemoryStream);

{
Don't need you anymore. We'll make a new one when the time comes.
}
ABitmap.Free;

bmpBuffer := MemoryStream.Memory;

{ Set PbmpInfoR to point to the source bitmap's info header. }
{ Boy, these headers are getting annoying. }
Inc( bmpBuffer, SizeOf(TBitmapFileHeader) );
PbmpInfoR := PBitmapInfoHeader(bmpBuffer);

{ Set bmpBuffer to point to the original bitmap bits. }
Inc(bmpBuffer, SizeOf(PbmpInfoR^));
{ Set the ColumnZero pointer to point to, uh, column zero. }
PbmpBufferColumnZero := bmpBuffer;

with PbmpInfoR^ do
begin
BytesPerPixel := biBitCount shr 3;
{ ScanLines are DWORD aligned. }
BytesPerScanLine := ((((biWidth * biBitCount) + 31) div 32) * SizeOf(DWORD));
BytesPerScanLineR := ((((biHeight * biBitCount) + 31) div 32) * SizeOf(DWORD));

{ The TMemoryStream that will hold the rotated bits. }
MemoryStreamR := TMemoryStream.Create;
{
Set size for rotated bitmap. Might be different from source size
due to DWORD aligning.
}
MemoryStreamR.SetSize(BITMAP_HEADER_SIZE + BytesPerScanLineR * biWidth);
end;

{ Copy the headers from the source bitmap. }
MemoryStream.Seek(0, soFromBeginning);
MemoryStreamR.CopyFrom(MemoryStream, BITMAP_HEADER_SIZE);

{ Here's the buffer we're going to rotate. }
bmpBufferR := MemoryStreamR.Memory;
{ Skip the headers, yadda yadda yadda... }
Inc(bmpBufferR, BITMAP_HEADER_SIZE);

{
Set up PbmpBufferRFirstScanLine and advance it to end of the first scan
line of bmpBufferR.
}
PbmpBufferRFirstScanLine := bmpBufferR;
Inc(PbmpBufferRFirstScanLine, (PbmpInfoR^.biHeight - 1) * BytesPerPixel);

{ Here's the meat. Loop through the pixels and rotate appropriately. }

{ Remember that DIBs have their origins opposite from DDBs. }
for Y := 1 to PbmpInfoR^.biHeight do
begin
PbmpBuffer := PbmpBufferColumnZero;
PbmpBufferR := PbmpBufferRFirstScanLine;

for X := 1 to PbmpInfoR^.biWidth do
begin
for T := 1 to BytesPerPixel do
begin
PbmpBufferR^ := PbmpBuffer^;
Inc(PbmpBufferR);
Inc(PbmpBuffer);
end;
Dec(PbmpBufferR, BytesPerPixel);
Inc(PbmpBufferR, BytesPerScanLineR);
end;

Inc(PbmpBufferColumnZero, BytesPerScanLine);
Dec(PbmpBufferRFirstScanLine, BytesPerPixel);
end;

{ Done with the source bits. }
MemoryStream.Free;

{ Now set PbmpInfoR to point to the rotated bitmap's info header. }
PbmpBufferR := MemoryStreamR.Memory;
Inc( PbmpBufferR, SizeOf(TBitmapFileHeader) );
PbmpInfoR := PBitmapInfoHeader(PbmpBufferR);

{ Swap the width and height of the rotated bitmap's info header. }
with PbmpInfoR^ do
begin
T := biHeight;
biHeight := biWidth;
biWidth := T;
end;

ABitmap := TBitmap.Create;

{ Spin back to the very beginning. }
MemoryStreamR.Seek(0, soFromBeginning);
ABitmap.LoadFromStream(MemoryStreamR);

MemoryStreamR.Free;
end;

winasm 2004-12-29
  • 打赏
  • 举报
回复
用IMAGE EN 控键
windows平台上支持BITMAPCOREHEADER、BITMAPINFOHEADER、BITMAPV4HEADER、BITMAPV5HEADER四种类型位图的类库! 简单用法举例: DibBitmap bmp,ret,tmp; bmp.Open(_T("E:\\bmp.bmp"));//打开 bmp.ConvertBit(4,ret);//转为4位位图,ret保存返回值 ret.RotateLeft(tmp);//向左旋转90度,tmp保存返回值 tmp.Invert();//所有像素反色,即RGB(1,2,3)变为RGB(254,253,252) tmp.At(10,10)=tmp.At(20,20);//单个像素点的取值和赋值操作 tmp.At(10,20)=ret.At(30,30);//注意,设置成的颜色如果颜色表内不存在,会设置成相近色 tmp.At(20,10)=RGB(255,0,0); bmp.AlphaBlend(0,0,tmp,128,RGB(255,255,255),true);//tmp以透明度128画到bmp的(0,0)处,其中tmp中的白色会镂空 bmp.Mirror();//bmp水平翻转 bmp.ConvertBit(16,ret,true);//转为5-6-5型16位位图,ret保存返回值 ret.SetClipbrd();//存入剪切板中 ret.Save(_T("E:\\bmp2.bmp"));//保存到文件 该类的头文件接口大致如下: class DibBitmap { BITMAPFILEHEADER* m_pbmfh; DWORD m_size;//保存m_pbmfh中malloc出来的内存大小,即是capacity//不为0时保证图像是可以处理的 public: enum BmType{NONE=0,CORETYPE,INFOTYPE,V4TYPE,V5TYPE}; public: class reference { //略.. }; public://DibBitmap01.cpp DibBitmap(); explicit DibBitmap(LPCTSTR pstrFileName); explicit DibBitmap(BITMAPFILEHEADER* pbmfh);//只是简单的赋值给成员变量m_pbmfh DibBitmap(HDC hdc, HBITMAP hBitmap, int BitCount);//1,4,8;16,24,32 DibBitmap(const DibBitmap& rhs); ~DibBitmap(); bool Open(LPCTSTR pstrFileName);//打开失败会Close() bool Save(LPCTSTR pstrFileName)const; void Close(); void Swap(DibBitmap& rhs); DibBitmap& operator=(const DibBitmap& rhs);//不一定申请过新内存 public://DibBitmap01.cpp BITMAPFILEHEADER* Get();//获取文件头 const BITMAPFILEHEADER* Get()const; BITMAPINFOHEADER* GetInfoHead();//获取信息头 const BITMAPINFOHEADER* GetInfoHead()const; RGBQUAD* GetQuad();//获取颜色表//没有颜色表,则返回NULL const RGBQUAD* GetQuad()const; BYTE* GetByte();//获取图素位 const BYTE* GetByte()const; bool GetMask(DWORD* dwMask,bool b565=false)const; //dwMask个数为四,需要屏蔽码时,依次存入RGB屏蔽码,并返回true //当为BITMAPV4HEADER以上时,还存入AlphaMask;b565参数同GetClr bool Attach(BITMAPFILEHEADER* pbmfh); BITMAPFILEHEADER* Detach();//返回值最后须free掉 HBITMAP CreateBitmap(HDC hdc)const;//返回值最后须DeleteObject掉 HBITMAP CreateDibSection ()const;//返回值最后须DeleteObject掉 BmType Type()const; DWORD Capacity()const;//返回m_size的值//注意,此值可能小于实际malloc的内存长度 DWORD FileSize()const;//整个文件的大小//不是返回m_size的值,m_size的值大于等于此返回值 DWORD BitsSize()const;//图素位的大小,即去掉文件头,信息头和颜色表之后的大小 DWORD Offset()const;//图素位的偏移量 LONG Width()const; LONG Height()const;//可能为负值 WORD Planes()const; WORD BitCount()const; DWORD ClrUsed()const; //一般是4,8位图才小于2^(4,8),其余的等于对应颜色数,m_pbmfh为空时返回0,32位图时,返回-1,16位555位图颜色数按565算 public://DibBitmap02.cpp LPTSTR DisplayDibHeader (LPTSTR szBuffer)const;//szBuffer得大于1200个//返回szBuffer RGBQUAD GetClr(size_t x,size_t y,bool b565=false)const; //获取(x,y)处的颜色值,已考虑方向问题,b565只有在16位深位图且没有屏蔽码时才有用,指明是5-5-5还是5-6-5 COLORREF GetRGBClr(size_t x,size_t y,bool b565=false)const;//条件同上//返回的最高位为0 bool GetClipbrd(HWND hwnd);//失败不一定会Close() void SetClipbrd(HWND hwnd)const; //If hwnd is NULL, the open clipboard is associated with the current task. void Mirror(bool bHoriz);//镜像//false表示垂直镜像 public://DibBitmap03.cpp void Invert(bool b565=false);//反色//在这里b565参数不起任何作用 RGBQUAD Invert(size_t x,size_t y,bool b565=false);//(x,y)处的颜色反色,返回反色后的近似颜色 RGBQUAD SetClr(size_t x,size_t y,RGBQUAD quad,bool b565=false);//设置颜色,返回设置成的最相近的颜色 COLORREF SetRGBClr(size_t x,size_t y,COLORREF clr,bool b565=false);//设置颜色,返回设置成的最相近的颜色 //以下两个函数相当牛逼,可直接引用修改颜色 COLORREF At(size_t x,size_t y,bool b565=false)const;//获取(x,y)处的颜色 reference At(size_t x,size_t y,bool b565=false);//获取(x,y)处的颜色引用,可直接进行修改 void AlphaBlend(int x,int y,const DibBitmap& rhs,BYTE bAlpha,COLORREF clr,bool bClr=false,bool b565=false); //把rhs以透明度bAlpha画到this中的(x,y)处(可以为负),注意:当&rhs==this时直接返回,啥也不操作 //若bClr为true,则clr参数有用,rhs中颜色值为clr的像素点完全透明 void RotateLeft(DibBitmap& ret)const; //this旋转(逆时针90度)后存入ret中,若this原先为空,则不改变ret,注意:当&ret==this时直接返回,啥也不操作 void RotateRight(DibBitmap& ret)const; //this旋转(顺时针90度)后存入ret中,若this原先为空,则不改变ret,注意:当&ret==this时直接返回,啥也不操作 void RotateOpposite();//旋转180度,注意后面没有const,此函数是旋转自身 public://DibBitmap04.cpp bool ConvertBit(WORD BitCount,DibBitmap& ret,bool b565=false)const; //转为同类型的不同位数位图//1,4,8;16,24,32,注意:当&ret==this时直接返回,啥也不操作 }; 说明: 此类支持四种位图格式: BITMAPCOREHEADER、BITMAPINFOHEADER、BITMAPV4HEADER、BITMAPV5HEADER。 该类像素点坐标序号从0开始,原点位于位图左上角,不管位图信息头的高度字段是否为负,内部都已作转化处理 暂不支持这四种位图的以下几种情况: 1、biCompression字段为BI_RLE4,BI_RLE8,BI_JPEG,BI_PNG的位图; 2、BITMAPV5HEADER时,bV5CSType字段等于PROFILE_LINKED或PROFILE_EMBEDDED时的位图。 有问题联系:hastings1986@163.com

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧