Results 1 to 9 of 9

Thread: [RESOLVED] Why does rotated Transparent image is not saved at its rotated degree with bg image

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2024
    Posts
    36

    Resolved [RESOLVED] Why does rotated Transparent image is not saved at its rotated degree with bg image

    Hello

    Why does rotated Transparent image is not saved at its rotated degree with background image below it ?
    Why does the following does not save as combination of two images into One ?

    Able to rotate the Transparent image in Picturebox1. How to save the same as modfied/Rotated image with desired degree ?

    1.) First of all How do i save the Transparent image(.PNG) with rotated as desired degree ?

    2.) I've also loaded another picture(.JPG) in Picturebox1 to represent as background.

    Ultimately the Transparent image rotates above the represented Background image

    I was trying to save both the images but only the represented Background Image is Saved and not with Transparent image.
    Why does Transparent Image which is on top of represented Background image does not get saved ?

    Code:
    Public Class Frm
    Private imgTransp As String = "C:\Images\imgTrans.png"
    Private myBGimg As String = "C:\BG-Images\BgImg1.JPG"
    
    
    Private Sub Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           Call FormLayout()    
    End Sub
    
    Private Sub FormLayout()
            PictureBox1.Size = New Size(800, 700)  
            PictureBox1.Image = Image.FromFile(imgTransp)
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    End Sub
    
    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
      Dim dgr As Double
      If dgr = 360 Then dgr = 0
         dgr += Val(txtBxDegrees.Text) 
         PictureBox1.Invalidate()    
      
      With e.Graphics
                .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
                .RotateTransform(CSng(dgr))
                Dim imgBmp = New Bitmap(imgTransp)
                imgBmp = ResizeImage(imgBmp, PictureBox1.Width, PictureBox1.Height)
                .DrawImage(imgBmp, -imgBmp.Width \ 2, -imgBmp.Height \ 2)
               'above rotating Transparent image
    'Below Background Image 
                Dim newImgBmp = New Bitmap(myBGimg)
                newImgBmp = ResizeImage(newImgBmp, PictureBox1.Width, PictureBox1.Height)
                .DrawImage(newImgBmp, newImgBmp.Width, newImgBmp.height)
            End With
    
    End Sub
    
    Public Shared Function ResizeImage(ByVal InputBitmap As Bitmap, width As Integer, height As Integer) As Bitmap
            Return New Bitmap(InputBitmap, New Size(width, height))
    End Function
    I am completely lost here because First of all How to save the Rotated(at desired degree) Transparent image(.PNG) over the background image
    Display both the images ie Rotated Image with Background in Picturebox1 and save as ".JPG"

    The below saves only the background image because I thought Image or combination of images in Picture1Box will save as is.
    Code:
    Private Sub btnSaveImg_Click(sender As Object, e As EventArgs) Handles btnSaveRotatedImg.Click
    
         Dim savedPath As String = "C:\SavedImages"
         Dim modifiedImage As New Bitmap(PictureBox1.Width, PictureBox1.Height)
         Dim gr As Graphics = Graphics.FromImage(modifiedImage)
    
         modifiedImage = PictureBox1.Image
         PictureBox1.Image.Save(savedPath & "\" & txtBxSaveFileName.Text & ".JPG", Drawing.Imaging.ImageFormat.JPG)
    
    End Sub
    What would be your take in saving both the images into one into which format?

    I went thru. the following links which i thought i could get insights and implement the appropriate but somehow did not succeed
    Stackoverflow:combing Two Images 1
    Vbforums:Rotaing Picture Saving it 2
    Vbforums: Overlapping images 3

    Thanks
    nkvb
    Last edited by nkvb; Sep 13th, 2024 at 01:38 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,691

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    You’re painting an overlay image ON your picturebox. I advised you not to do that…

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2024
    Posts
    36

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    .Paul
    You’re painting an overlay image ON your picturebox. I advised you not to do that…
    Agreed. Revised code below completely removed that part in Paint_Event
    Code:
    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
      Dim dgr As Double
      If dgr = 360 Then dgr = 0
         dgr += Val(txtBxDegrees.Text) 
         PictureBox1.Invalidate()    
      
      With e.Graphics
                .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
                .RotateTransform(CSng(dgr))
                Dim imgBmp = New Bitmap(imgTransp)
                imgBmp = ResizeImage(imgBmp, PictureBox1.Width, PictureBox1.Height)
                .DrawImage(imgBmp, -imgBmp.Width \ 2, -imgBmp.Height \ 2)
               'above rotating Transparent image
            End With
    End Sub
    Why is the rotated image not being taken into a/c when saving it
    How do i save the image with desired degree rotated ?

    nkvb

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,691

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    Painting ON the PictureBox is NOT the same as painting on a Bitmap IN the PictureBox

    Don't even touch the PictureBox1_Paint eventhandler. This is how to do it...

    Code:
    Public Class Form3
    
        Dim dgr As Single = 0
        Dim img1 As Bitmap = Nothing
    
        Private Sub txtBxDegrees_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBxDegrees.TextChanged
            Dim s As Single
            If Single.TryParse(txtBxDegrees.Text, s) Then
                If dgr = 360 Then dgr = 0
                dgr += s
    
                img1 = New Bitmap([your background image path])
                img1 = ResizeImage(img1, PictureBox1.Width, PictureBox1.Height)
                Dim gr As Graphics = Graphics.FromImage(img1)
                
                With gr
                    .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
                    .RotateTransform(dgr)
                    Dim img2 = New Bitmap([your foreground image path])
                    img2 = ResizeImage(img2, PictureBox1.Width, PictureBox1.Height)
                    .DrawImage(img2, -img2.Width, -img2.height)
                End With
    
                PictureBox1.Image = img1
            End If
        End Sub
    
        Public Shared Function ResizeImage(ByVal InputBitmap As Bitmap, ByVal width As Integer, ByVal height As Integer) As Bitmap
            Return New Bitmap(InputBitmap, New Size(width, height))
        End Function
    
        Private Sub btnSaveImg_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSaveRotatedImg.Click
    
            Dim savedPath As String = "C:\SavedImages"
            If Not img1 Is Nothing Then
                img1.Save(savedPath & "\" & txtBxSaveFileName.Text & ".JPG", Drawing.Imaging.ImageFormat.JPG)
            End If
    
        End Sub
    
    End Class
    Last edited by .paul.; Sep 14th, 2024 at 06:33 AM.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2024
    Posts
    36

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    .Paul

    Indeed more clarity has been explained. You are right Sir,
    Painting ON the PictureBox is NOT the same as painting on a Bitmap IN the PictureBox

    Don't even touch the PictureBox1_Paint eventhandler. This is how to do it...
    Before implementing your code with TextChanged Event.

    the doubt raised Why TextChanged Event ?

    Won't the Front image(img2) rotate as per the ADDED numeric value in txtBxdegrees.Text
    Deep Observation made here is
    for eg
    When I enter 9 the img2 moves to 9 degrees then enter 0 to make it 90 the Img2 rotates to 99 degrees. Instead of rotating to 90. You may check
    Then ones clear the textbox and Enter 45 it rotates to 144 ie (99+45=144) instead of rotating back to 0 degrees and then to 45

    FYI as per Paint Event, which I've deleted now, above comments were not existing. You have empty textbox then img2 to rotate back to 0 degrees.

    Can we control this ?

    Saved One image with above oversight added degrees rotation and observation was made after that

    nkvb
    Last edited by nkvb; Sep 15th, 2024 at 03:00 AM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,691

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    dgr isn't reset to 0 when you clear your textbox

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2024
    Posts
    36

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    dgr isn't reset to 0 when you clear your textbox
    This is my first time coming across .TryParse. Did Explore many examples but was not successful
    I tried in the below following manner
    Code:
        If Single.TryParse(txtBxDegrees.Text, s) Then
                If String.IsNullOrEmpty(txtBxDegrees.Text) Then
                    dgr = 0
                Else
                    dgr += s 
                End If
          ......
       End if
    nkvb

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,691

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    Code:
    If Single.TryParse(txtBxDegrees.Text, s) Then
        dgr += s
    
        img1 = New Bitmap([your background image path])
        img1 = ResizeImage(img1, PictureBox1.Width, PictureBox1.Height)
        Dim gr As Graphics = Graphics.FromImage(img1)
                
        With gr
            .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
            .RotateTransform(dgr)
            Dim img2 = New Bitmap([your foreground image path])
            img2 = ResizeImage(img2, PictureBox1.Width, PictureBox1.Height)
            .DrawImage(img2, -img2.Width, -img2.height)
        End With
    
        PictureBox1.Image = img1
    Else
        dgr = 0 
    End If

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2024
    Posts
    36

    Re: Why does rotated Transparent image is not saved at its rotated degree with bg ima

    .Paul Sir

    Thank you so much for helping me for resolving my issue

    Thank you once more.

    nkvb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width