B4J Question Capture part of an ImageView's image and return it as an image

Christos Dorotheou

Member
Licensed User
Longtime User
Hi there,

I am trying to capture a part of an image displayed with an Imageview and then convert that part
as a new image in an other ImageView.

The dimensions of the image's section that I need to capture are 512 Wide and 384 Height
at the starting location 100,279

I have tried to accomplish it as follows but with no success.

Any help or ideas will be greatly appreciated.



B4X:
Sub create_outer_hatch
    Dim btc As ByteConverter
    Dim pix(513*385) As Int
    Dim imgbuff As Image = IV_CoverImage.GetImage
    Dim IVouterhatch_img1 As ImageView
    Dim x1, x2, y1, y2, idx As Int
   
    x1 = 100
    y1 = 279
    x2 = x1 + 512
    y2= y1 + 384
   
    idx = -1
    For x = x1 To x2
        For y = y1 To y2
            idx = idx + 1
            pix(idx) = imgbuff.GetPixel(x, y)
        Next
    Next

    IVouterhatch_img1.Initialize("")
    IVouterhatch_img1.SetImage(BytesToImage(btc.IntsToBytes(pix)))
    
    MainForm.RootPane.AddNode(IVouterhatch_img1,0,0,512,384)
   
End Sub

Public Sub BytesToImage(bytes() As Byte) As Image
   Dim In As InputStream
   In.InitializeFromBytesArray(bytes, 0, bytes.Length)
   Dim bmp As Image
   bmp.Initialize2(In)
   Return bmp
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to crop an image:
B4X:
Sub CropImage(img As Image, Left As Int, Top As Int, Width As Int, Height As Int) As Image
   Dim cvs As Canvas
   cvs.Initialize("")
   MainForm.RootPane.AddNode(cvs, 0, 0, Width, Height)
   cvs.DrawImage2(img, Left, Top, Width, Height, 0, 0, Width, Height)
   Dim r As Image = cvs.Snapshot
   cvs.RemoveNodeFromParent
   Return r
End Sub
 
Upvote 0
Top