flip bitmap horizontally and vertically

derez

Expert
Licensed User
Longtime User
Canvas can draw a bitmap straight or rotated, but not to flip it vertically or horizontally.

I use this code to do it - pixel by pixel.

I wish for a drawing command (or a better code - this one is very slow).

Thanks.

B4X:
Dim flipbmp As Bitmap
Dim flipcnvs As Canvas
ProgressDialogShow("....")
flipbmp.Initialize3(clipiv.Bitmap)
flipcnvs.Initialize2(flipbmp)
If flipflag = 1  Then             ' flip  vertical
   For i = 0 To clipiv.Width - 1
     For j = 0 To clipiv.Height - 1
       flipcnvs.DrawCircle(i,clipiv.Height - j,1,clipcnvs.Bitmap.GetPixel(i,j),True,1)
     Next
     DoEvents
  Next
.....
 

derez

Expert
Licensed User
Longtime User
Actually I found myself a better code - I copy the bitmap line by line instead of by pixels, the lines are vertical for horizontal flip and vise versa, so there is one for loop only.
Copying the lines is by defining source and dest rectangles and using canvas.drawbitmap.

In the attached code, clipiv is the source and rectpnl is the place to copy the result to.

B4X:
Dim flipsrc, flipdst As Rect
If flipflag = 1  Then             ' flip  vertical
   For j = 0 To clipiv.height - 2
      flipsrc.Initialize(0,j,clipiv.width,j+1)
      flipdst.Initialize(rectpnl.Left,rectpnl.Top+ clipiv.Height-j-1,rectpnl.Left+rectpnl.Width,rectpnl.Top + clipiv.Height-j)
      cnvs.DrawBitmap(clipiv.Bitmap,flipsrc,flipdst)
   Next
Else                               'flip horizontal
   For i = 0 To clipiv.width - 2
      flipsrc.Initialize(i,0,i+1,clipiv.Height)
      flipdst.Initialize(rectpnl.Left + clipiv.Width - i - 1,rectpnl.Top,rectpnl.Left+ clipiv.width-i,rectpnl.Top+rectpnl.Height )
      cnvs.DrawBitmap(clipiv.Bitmap,flipsrc,flipdst)
   Next
End If
 
Last edited:
Top