How to save part of a bitmap

MaxApps

Active Member
Licensed User
Longtime User
Hi

I am trying to save a part of a bitmap, but I cant get it right.
The following program almost does it, but there is one BIG problem.
The part of the image, that I am saving, should be 96dip x 64dip. That small part does get saved, but it gets filled with black around it and the bitmap ends up with 480x465.
Can someone tell me what is wrong?

canvas1.Initialize(Panel1)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "1.jpg")
Dim DestRect As Rect
DestRect.Initialize(0dip, 0dip, 96dip, 64dip)

Dim name As String
Dim Out As OutputStream

Dim SrcRect As Rect
Dim Nummer As Int
SrcRect.Initialize(0, 0, 96, 64)
DestRect.Top = 0dip
DestRect.Bottom = 64dip
DestRect.Left = 0dip
DestRect.Right = 96dip
canvas1.DrawBitmap(Bitmap1, SrcRect, DestRect)
name="part1.png"
Out = File.OpenOutput(File.DirRootExternal, name, False)
canvas1.Bitmap.WriteToStream(Out, 100, "PNG")
Out.Close


Kind regards
Jakob
 

MaxApps

Active Member
Licensed User
Longtime User
I have now added:


Panel1.Height=64dip
Panel1.Width=96dip

And that helped a lot. Now the bitmap ends up being 144x96
Why not 96x64?
 
Upvote 0

MaxApps

Active Member
Licensed User
Longtime User
I dont know how to do that.
I cant write img.drawbitmap or img.bitmap.writetostream

Kind regards
Jakob
 
Upvote 0

mkvidyashankar

Active Member
Licensed User
Longtime User
How to Save the image which is visible on the imageview1

Hi

I am trying to save a bitmap which is visible on the imageview.

image is loaded to imageview from a selected file

then image is adjusted using pan and zoom ( using imageviewextras library)

the visible image should be saved.

I am trying to use canvas, but the whole image is saving again

please guide....:sign0085:
 
Upvote 0

mkvidyashankar

Active Member
Licensed User
Longtime User
How to save Displayed image

Hi

Here is the code

B4X:
Sub imageset_click
  chooser.Show("image/*", "Choose image")
End Sub



Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    seldir=Dir
   selfile=FileName
    If Success Then
       Panel_SELECTIMAGE.Visible=True
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
      MyImageviewExtras.setBitmap(image_SELECTED,bmp)
      MyImageviewExtras.enablePanAndZoom(image_SELECTED)
      'image_SELECTED.Bitmap=LoadBitmap(Dir, FileName)
      
      'rc.writeBitmapToFile(bmp, filedefault,
      
      'Activity.SetBackgroundImage(bmp)
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

After Zoom and pan a button is provided to save the displayed (pan and zoomed) image
here it is
B4X:
Sub Button_ok_Click
        Dim newfile As String
      Dim bmap2 As Bitmap
      Dim cs As Canvas
      'cs.Initialize(image_SELECTED)
      'bmap2.InitializeMutable(image_SELECTED.Width,image_SELECTED.Height)
'      'bmap2=image_SELECTED..Bitmap
'        Dim path1 As Path
'      path1.Initialize(image_SELECTED.left,image_SELECTED.top)
'      path1.LineTo(image_SELECTED.left+image_SELECTED.width,image_SELECTED.Top)
'      path1.LineTo(image_SELECTED.left+image_SELECTED.width,image_SELECTED.Top+image_SELECTED.Height)
'      path1.LineTo(image_SELECTED.left,image_SELECTED.Top+image_SELECTED.Height)
'      path1.Initialize(image_SELECTED.left,image_SELECTED.top)
'      cs.ClipPath(path1)
'      
      bmap2.Initialize3(image_SELECTED.Bitmap)
      newfile=DateTime.Now & ".jpg"
      
      rc.writeBitmapToFile(bmap2,filedefault,newfile,100)
       'File.Copy(seldir,selfile,filedefault,newfile)
      map2.Put("filepath",filedefault)
      map2.Put("imagefile",newfile)
      File.WriteMap(File.DirInternal, "alarmimage.ini", map2)
End Sub

Sub Button_cancel_Click
      ToastMessageShow("No image selected", True)
      Panel_SELECTIMAGE.Visible=False
End Sub
 
Upvote 0

mkvidyashankar

Active Member
Licensed User
Longtime User
here is the file

Here is the file

I could not upload the whole project as it exceeds available limit
so i separated this module and uploaded

I am using ImageviewExtra to add pan and zoom functions to imageview
the main purpose of this module is to save selected part of the image which is displayed on the imageview

Please guide to solve the problem
 
Last edited:
Upvote 0

mkvidyashankar

Active Member
Licensed User
Longtime User
hi klaus

please download the updated zip file. I am using core, dialog, imageviewextras and phone library for this module
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I had a look at your program.
You are using the ImageViewExtra library, but this one is no more available and replaced by the TouchViewExtra library.
The two functions you are using setBitmap and enablePanAndZoom are not in the new library. So I can't test the program in the same conditions.
Did you test the program you last posted ?
What are you doing with the MyImageviewExtras object ?

Best regards.


Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…