Results 1 to 6 of 6

Thread: Picturebox (loadpicture with an offset)

  1. #1

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Picturebox (loadpicture with an offset)

    I am using the following line of code:

    Code:
    pic1.Picture = LoadPicture("C:\test\OUTPUT.BMP")
    ...This works good. But now the image's height I'm loading is too high. The important part of the picture is then partially or entirely invisible in my picturebox (too low).

    I don't want to change the dimension of the picturebox, and I don't want to edit the images.

    Would there be a way to load the picture with an offset setting? I tried to play with LoadPicture parameters but it didn't seem to help.

    Thanks for any solution.
    Chris

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Picturebox (loadpicture with an offset)

    Create an invisible picturebox, then load your image into it. Then you can use the PaintPicture, to simply paint the content partially, to the pic1 control.

    Or, you can place an another picturebox, inside pic1, so you load the picture into the inner picbox, that is you can resize, then move the the negative position, so it will simply show you the content you are after.

  3. #3

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: Picturebox (loadpicture with an offset)

    Your paintpicture idea was the best as I didn't want to use additional pictureboxes on the form (even invisible) and I didn't need it 'aesthetic' at all neither.

    Also, and I forgot to mention, the offset needed is static. I don't need to evalute the picture and move it accordingly.

    That did it:

    Code:
    pic1.PaintPicture pic1.Picture, 0, -30
    Thank you.
    Chris

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Picturebox (loadpicture with an offset)

    Why don't you use a scrollbar ?

    Check out this link...

    http://www.vbforums.com/showthread.php?t=557689
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Picturebox (loadpicture with an offset)

    Here is a way without using an additional control. It will proportionally scale the loaded picture to the size of your picturebox. This uses the .Render method of the stdPicture object.
    Code:
    Private Sub Command1_Click()
        Dim tPic As StdPicture
        Dim xRatio As Single, yRatio As Single
        Dim dstCX As Long, dstCY As Long
        Dim srcCX As Long, srcCY As Long
        
        On Error Resume Next
        Set tPic = LoadPicture(strFileName) ' supply valid path/filename
        If Err Then 
            Err.Clear
            Exit Sub
        End If
        On Error Goto 0
        
        With Picture1
            .Cls
            .AutoRedraw = True
            dstCX = .ScaleX(.ScaleWidth, .ScaleMode, vbPixels) ' convert picbox size to pixels
            dstCY = .ScaleY(.ScaleHeight, .ScaleMode, vbPixels)
        End With
        
        With tPic
            srcCX = Picture1.ScaleX(.Width, vbHimetric, vbPixels) ' convert stdPic size to pixels
            srcCY = Picture1.ScaleY(.Height, vbHimetric, vbPixels)
            xRatio = dstCX / srcCX
            yRatio = dstCY / srcCY
            If yRatio < xRatio Then xRatio = yRatio
            dstCX = srcCX * xRatio
            dstCY = srcCY * xRatio
            ' Draw to scale. Don't remove the +0& below else errors
            .Render Picture1.hDC, 0&, 0&, dstCX + 0&, dstCY + 0&, _
                0&, .Height, .Width, -.Height, ByVal 0&
        End With
        Picture1.Refresh
    
    End Sub
    Last edited by LaVolpe; Feb 20th, 2009 at 10:52 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Picturebox (loadpicture with an offset)

    See my previous reply also. In that reply I offered a way to scale the image to your picturebox, but you wanted to offset it, so with slight modifications of the previous post, we have the following
    Code:
    Private Sub LoadAndOffset(FileName As String, Destination As VB.PictureBox, xOffset As Long, yOffset As Long)
        Dim tPic As StdPicture
        Dim srcCX As Long, srcCY As Long
        
        On Error Resume Next
        Set tPic = LoadPicture(FileName)
        If Err Then 
            Err.Clear
            Exit Sub
        End If
        On Error Goto 0
        
        With Destination  ' the .Cls & .AutoRedraw can be done outside of this routine if desired; does not have to be done here
            .Cls
            .AutoRedraw = True
        End With
        
        With tPic
            srcCX = Destination.ScaleX(.Width, vbHimetric, vbPixels)
            srcCY = Destination.ScaleY(.Height, vbHimetric, vbPixels)
    
            ' Draw to scale. Don't remove the +0& below else errors
            .Render Destination.hDC, xOffset + 0&, yOffset + 0&, srcCX + 0&, srcCY + 0&, _
                0&, .Height, .Width, -.Height, ByVal 0&
        End With
        Destination.Refresh 'this can be called outside of this routine; does not need to be done here
    
    End Sub
    Edited: The +/- offsets must be passed as pixels.
    Last edited by LaVolpe; Feb 20th, 2009 at 11:54 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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