Resizing bitmap produces mottled background

Widget

Well-Known Member
Licensed User
Longtime User
If I use Canvas.DrawBitmap it seems to produce a mottled (herring bone) background on the resized image. If I load the bitmap directly to the panel (or imageview) the image background is quite sharp, even though the Views they are the same size as the bitmap.

Why doesn't DrawBitmap produce a sharp background? Is there a better way to resize a bitmap? I'd appreciate it if someone can shed some light onto what I'm doing wrong. Here is my code:

B4X:
Sub ResizeBitmap(aBitmap As Bitmap,  aWantWid As Int, aWantHt As Int, aKeepAspectRatio As Boolean) As Bitmap
  If aBitmap.Width <= aWantWid AND aBitmap.Height <= aWantHt Then
  Else 
    If aKeepAspectRatio Then
      If aBitmap.Width / aWantWid > aBitmap.Height / aWantHt Then
        If aBitmap.Width < aWantWid Then aWantWid = aBitmap.Width
        aWantHt = aBitmap.Height * aWantWid/aBitmap.Width
      Else
        If aBitmap.Height < aWantHt Then aWantHt = aBitmap.Height
        aWantWid = aBitmap.Width * aWantHt/aBitmap.Height
      End If
    End If
  End If

  Dim bmpNew As Bitmap
  bmpNew.InitializeMutable(aWantWid, aWantHt)
    
  Dim cnv As Canvas
  cnv.Initialize2(bmpNew)
  Dim destRect As Rect
  destRect.Initialize(0, 0, aWantWid, aWantHt)
  cnv.DrawBitmap(aBitmap, Null, destRect)
  Return bmpNew
End Sub

TIA
Widget
 

Kimmowich

Member
Licensed User
Longtime User
try this:

Sub ResizeBM(bm As Bitmap, width As Int, height As Int, KeepRatio As Boolean) As Bitmap
Dim newHeight As Int
newHeight = Height
If KeepRatio Then
Dim CalcRatio As Int
CalcRatio = bm.Width / bm.Height
newHeight = width / CalcRatio
End If

Dim r As Rect
Dim b As Bitmap
Dim c As Canvas
r.Initialize(0,0, width, newheight)
b.InitializeMutable(width, newheight)
c.Initialize2(b)

c.DrawBitmap(BM, Null, r)
Return b
End Sub
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
Trying to use this Sub, I get "IllegalArgumentException: bitmap size exceeds 32 bits".
If I don't use KeepRatio, it works.
 
Upvote 0
Top