Pakistan IT Forum (Discuss Information Technology in Pakistan)
News: Make new friends at our new Community Website ToddSpace.com Here
 
*
Hi, Guest. Welcome to IT Forum of Pakistan, Please login or register. November 21, 2008, 06:36:12 AM


Login with IT Forum's username, password and session length


Pak IT forum is launched to provide Pakistan's IT community with a platform to discuss IT related issues. These IT related discussions are proved to be useful for other Pakistani people. Information Technology in Pakistan is hottest issue on IT related forums. We hope that serious Pakistani people will join this IT Forum and will let us know about their experience in order to boast Pakistan's Software Exports.

Pages: [1]
  Print  
Author Topic: resizing picture...great problem  (Read 233 times)
aamir100
Newbie
*
Posts: 1


View Profile Email
« on: September 09, 2008, 09:38:40 PM »

These are variables which I use for adjusting the picture into picture box and on resizing the form and these variables are also used in zooming function but not directly there!
Code:
Dim Auto_Size As Boolean = True
    Dim Shrink As Boolean = True
    Dim Enlarge As Boolean = True
    Dim ownerPnl As New Panel

This is the function which I pass it to the another function which is used to load picture in frmPic form first time
Code:
Private Sub DrawToScale(ByVal bmp As Image)
        On Error Resume Next
        pb.Image = New Bitmap(bmp)
        pb.SizeMode = PictureBoxSizeMode.Normal
        If Auto_Size Then Pic2Pic(pb, Panel1, Enlarge, Shrink)
        'pb=picturebox
        'Panel1=panel1
    End Sub

This is for resizing the form
Code:
Private Sub PopupForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        On Error Resume Next
        If Auto_Size Then Pic2Pic(pb, Panel1, Enlarge, Shrink)
    End Sub

Code for Pic2Pic Function
Code:
Public Sub Pic2Pic(ByVal Pic As PictureBox, ByVal Pnl As Panel, Optional ByVal FitSmall As Boolean = True, Optional ByVal FitBig As Boolean = True, Optional ByVal Border_Size As Double = 0)
        If Pic.Image Is Nothing Then Exit Sub
        Dim Pos As New ImagePosition()
        If isBigger(Pic, Pnl, True) Then
            If FitBig Then
                Pos.SetPosition(Pnl, Pic)
            Else
                Pic.SizeMode = PictureBoxSizeMode.AutoSize
                Move2Center(Pic, Pnl)
            End If
        Else
            If FitSmall Then
                Pos.SetPosition(Pnl, Pic)
            Else
                Pic.SizeMode = PictureBoxSizeMode.AutoSize
                Move2Center(Pic, Pnl)
            End If
        End If
        'add border
        If Border_Size > 1 Then
            Pic.Width /= Border_Size
            Pic.Height /= Border_Size
            Move2Center(Pic, Pnl)
        End If
    End Sub
Code for isBigger function
Code:
Public Function isBigger(ByVal pic As PictureBox, ByVal pnl As Panel, ByVal compare_Image As Boolean) As Boolean
        If compare_Image Then
            If pic.Image.Width > pnl.Width Or _
                        pic.Image.Height > pnl.Height Then
                Return True
            Else
                Return False
            End If
        Else
            If pic.Width > pnl.Width Or _
                        pic.Height > pnl.Height Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

Code for Move2Center. This function moves this picture in the center
Code:
Public Sub Move2Center(ByVal Pic1 As Object, ByVal Pic2 As Object, Optional ByVal Inborder As Boolean = True)
        Dim NX, NY As Integer
        NX = (Pic2.Width - Pic1.Width) / 2
        NY = (Pic2.Height - Pic1.Height) / 2
        If Inborder Then
            If NX < 0 Then NX = 0
            If NY < 0 Then NY = 0
        End If
        Pic1.Left = NX
        Pic1.Top = NY
    End Sub

This is MovePicto function.

Code:
Public Sub MovePicto(ByVal pic As PictureBox, ByVal newx As Single, ByVal newy As Single)
        If pic.Width > ownerPnl.Width Then
            If newx + pic.Width > ownerPnl.Width And newx < 0 Then
                pic.Left = newx
            Else
                If newx < 0 Then
                    pic.Left = ownerPnl.Width - pic.Width
                Else
                    pic.Left = 0

                End If
            End If
        Else
            pic.Left = (ownerPnl.Width - pic.Width) / 2
        End If
        If pic.Height > ownerPnl.Height Then
            If newy + pic.Height > ownerPnl.Height And newy < 0 Then
                pic.Top = newy
            Else
                If newy < 0 Then
                    pic.Top = ownerPnl.Height - pic.Height
                Else
                    pic.Top = 0
                End If
            End If
        Else
            pic.Top = (ownerPnl.Height - pic.Height) / 2
        End If
    End Sub
These are ZoomIn() and ZoomOut() functions
Code:
Private Sub ZoomIn()
        Dim w, h As Integer
        pb.SizeMode = PictureBoxSizeMode.StretchImage
        w = pb.Width
        h = pb.Height
        pb.Width = pb.Width * 1.1
        pb.Height = pb.Height * 1.1
        Dim x = pb.Left - (pb.Width - w) / 2
        Dim y = pb.Top - (pb.Height - h) / 2
        MovePicto(pb, x, y)
        Move2Center(pb, Panel1)
    End Sub

Public Sub ZoomOut()
        Dim w, h As Integer
        pb.SizeMode = PictureBoxSizeMode.StretchImage
        w = pb.Width
        h = pb.Height
        pb.Width = pb.Width / 1.1
        pb.Height = pb.Height / 1.1
        Dim x = pb.Left - (pb.Width - w) / 2
        Dim y = pb.Top - (pb.Height - h) / 2
        MovePicto(pb, x, y)
        Move2Center(pb, Panel1)
    End Sub

There is a class which is used for image position

Code:
Public Class ImagePosition
    Public Sub SetPosition(ByVal picPanel As Panel, ByVal picResim As PictureBox, Optional ByVal Param As String = "keepratio")

        Dim X, Y, ScrX, ScrY, PosX, PosY, PicX, PicY As Integer
        Dim ratio As Double

        Try
            ' picResim.Visible = False 'Hide picture

            PicX = picResim.Image.Width 'get pictures width
            PicY = picResim.Image.Height 'get pictures height
            ratio = PicX / PicY 'calculate aspect ratio

            ScrX = picPanel.Width  'get containers width
            ScrY = picPanel.Height  'get containers height

            'picPanel.AutoScroll = False

            Select Case LCase(Param)
                Case "normal" 'if normal
                    picPanel.AutoScroll = True 'add scrool bars
                    picResim.Width = PicX 'adjust pictures width
                    picResim.Height = PicY 'and height to the original sizes.
                    picResim.SizeMode = PictureBoxSizeMode.Normal  'Change to normal mode
                    If (picResim.Width > picPanel.Width) And (picResim.Height > picPanel.Height) Then 'if picture larger than container
                        PosX = 0 'set position X
                        PosY = 0 'set position Y
                    End If
                    If (picResim.Width > picPanel.Width) And (picResim.Height < picPanel.Height) Then 'if picture wider than container
                        PosX = 0
                        Y = picResim.Height 'get height
                        PosY = CInt((ScrY - Y) / 2) 'calculate position Y
                    End If
                    If (picResim.Width < picPanel.Width) And (picResim.Height > picPanel.Height) Then 'if picture taller than container
                        PosY = 0
                        X = picResim.Width  'get width
                        PosX = CInt((ScrX - X) / 2) 'calculate position Y
                    End If
                    If (picResim.Width < picPanel.Width) And (picResim.Height < picPanel.Height) Then 'if picture smaller than container
                        X = picResim.Width 'get width
                        Y = picResim.Height 'get height
                        PosX = CInt((ScrX - X) / 2) 'calculate position X
                        PosY = CInt((ScrY - Y) / 2) 'calculate position Y
                    End If
                Case "fit"
                    picResim.Width = ScrX 'adjust pictures width
                    picResim.Height = ScrY 'and height to the form sizes.
                    picResim.SizeMode = PictureBoxSizeMode.StretchImage
                    PosX = 0 'set position X
                    PosY = 0 'set position Y

                Case "center"
                    picResim.Width = PicX 'adjust pictures width
                    picResim.Height = PicY 'and height to the form sizes.
                    picResim.SizeMode = PictureBoxSizeMode.CenterImage
                    X = picResim.Width 'get width
                    Y = picResim.Height 'get height
                    PosX = CInt((ScrX - X) / 2) 'calculate position X
                    PosY = CInt((ScrY - Y) / 2) 'calculate position Y

                Case "keepratio"
                    If PicX >= PicY Then 'check whether pic. width greater than height
                        picResim.Width = ScrX 'yes, then main size is width
                        picResim.Height = CInt(picResim.Width / ratio) 'calculate height with aspect ratio
                        If picResim.Height > ScrY Then
                            picResim.Height = ScrY 'no, then main size is height
                            picResim.Width = CInt(picResim.Height * ratio) 'calculate width with aspect ratio
                        End If
                    Else
                        picResim.Height = ScrY 'no, then main size is height
                        picResim.Width = CInt(picResim.Height * ratio) 'calculate width with aspect ratio
                        If picResim.Width > ScrX Then
                            picResim.Width = ScrX 'yes, then main size is width
                            picResim.Height = CInt(picResim.Width / ratio) 'calculate height with aspect ratio
                        End If
                    End If
                    picResim.SizeMode = PictureBoxSizeMode.StretchImage 'resize the picture
                    X = picResim.Width 'get width
                    Y = picResim.Height 'get height
                    PosX = CInt((ScrX - X) / 2) 'calculate position X
                    PosY = CInt((ScrY - Y) / 2) 'calculate position Y
                Case Else
            End Select

            picResim.Left = PosX 'adjust the position()
            picResim.Top = PosY

            picResim.Visible = True 'show picture
        Catch
        End Try

    End Sub
End Class

Purpose of this code is to show all the functions and classes bcoz all functions are being called  from each other.

So if I use this code then picture is fit into the picture box when first a thumbnail is clicked and a form is displayed and on resizing the form it also fits in the picture box  but when I go to edit menu and open resize picture dialog box and enter height and width  and press ok button then this picture is not resized according to the entered height and width but picture is shrinking and it is displayed in bad quality and so no resizing is performed. But I have an other function which can resize the picture with mouse handling, catching the picture from one of its side or one its corner and drag them in and out so picture is resized up to desired size.
Code:
Private Sub DrawToScale(ByVal bmp As Image)
        On Error Resume Next
        pb.Image = New Bitmap(bmp)
        pb.SizeMode = PictureBoxSizeMode.Normal
        If Auto_Size Then Pic2Pic(pb, Panel1, Enlarge, Shrink)
    End Sub

This is for resizing the form
Code:
Private Sub PopupForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        On Error Resume Next
        If Auto_Size Then Pic2Pic(pb, Panel1, Enlarge, Shrink)
    End Sub

But if I remove following line from both the codes given above then picture is not fit into the picture into picture box and it’s some portion is not displayed when picture is loaded into form and the same case for form resizing. But picture resizing is performed successfully when  I go to edit menu and open picture resize dialog box and enter height and width and press ok button then picture is resized according to entered values. But if I zoom in or zoom out the picture the again picture shrinking problem occurs and also when I try to resize it with mouse then it is not resizing well, it is not resizing from corners but from only one corner and only from one side  and it also seems that as I cropping the picture but not resizing .resizing handle is appear at top left corner and top and left sides. If I resize it through dialog box and picture size is small then it is only moving but not resizing and if picture size is bigger then it is showing strange behavior.
The line which I remove is
Code:
If Auto_Size Then Pic2Pic(pb, Panel1, Enlarge, Shrink)
Now above code becomes
Code:
Private Sub DrawToScale(ByVal bmp As Image)
        On Error Resume Next
        pb.Image = New Bitmap(bmp)
        pb.SizeMode = PictureBoxSizeMode.Normal
    End Sub

This is for resizing the form
Code:
Private Sub PopupForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        On Error Resume Next
    End Sub

So what should I do now. I want to perform both the tasks perfectly. I want to fit the picture in picture box, I want to do resizing and I also want to do zooming. I want to resize the picture with mouse perfectly. What changes I should make to overcome these issues.??
This is the detail which I have given….if full code is needed then I can upload it also.
See attachment for more details.
thanks
Logged
Pages: [1]
  Print  
 
Jump to:  


Powered By: comsdev Partners