Results 1 to 16 of 16

Thread: [RESOLVED] Urrgh! Someone put an end to my misery please?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Resolved [RESOLVED] Urrgh! Someone put an end to my misery please?

    I want to load a png into a picture box and I've searched everywhere and tested every source code/project. But the problem is:
    WHY THE F**K NO ONE PROVIDES A STRAIGHT FORWARD PROJECT?

    I've seen projects with 10 classes, a lot of options,examples, confusing code structures, it was impossible to learn anything (especially from LaVolpe's GDI+ project). I tried to remove lines of codes so that I could be left with only the useful lines of codes needed to launch a common dialog control, open the png file and load it in the picturebox, but nooo, that was just impossible to do. I have been working on this form more than 6 hours. 6 HOURS! Stayed up all night, and the only stuff that I learned so far is that I need to use GDI+ and some classes such as c32bpp etc. Nothing more, because studying the code was impossible.

    Now, my question is:
    Can someone be so kind to provide me with a project which shows how to load a png file into a picture box? I only need a command button, a common dialog control and a picture box, nothing more,nothing less.

    HUGE THANK YOU

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,126

    Re: Urrgh! Someone put an end to my misery please?

    As BW points out, and I learned earlier:
    Code:
    Option Explicit
    
    'Uses Microsoft Windows Image Acquisition Library v2.0  (add in References)
    Private Sub Command1_Click()
        With New WIA.ImageFile
            .LoadFile App.Path & "\sample.png"
            Set Picture1.Picture = .FileData.Picture
        End With
    End Sub

    EDIT---Sorry, used app.path instead of CommonDialog.......

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,126

    Re: Urrgh! Someone put an end to my misery please?

    With commondialog:
    Code:
    Option Explicit
    
    'Uses Microsoft Windows Image Acquisition Library v2.0
    Private Sub Command1_Click()
         CommonDialog1.ShowOpen
         Dim myPNGFile As String
         myPNGFile = CommonDialog1.FileName
        With New WIA.ImageFile
            .LoadFile myPNGFile
            Set Picture1.Picture = .FileData.Picture
        End With
    End Sub
    EDIT---PS---really don't need profinity (even 'hidden') on this forum...shows lack of....well, you know....

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by SamOscarBrown View Post
    With commondialog:
    Code:
    Option Explicit
    
    'Uses Microsoft Windows Image Acquisition Library v2.0
    Private Sub Command1_Click()
         CommonDialog1.ShowOpen
         Dim myPNGFile As String
         myPNGFile = CommonDialog1.FileName
        With New WIA.ImageFile
            .LoadFile myPNGFile
            Set Picture1.Picture = .FileData.Picture
        End With
    End Sub
    EDIT---PS---really don't need profinity (even 'hidden') on this forum...shows lack of....well, you know....
    I didn't want to use WIA because for some strange reason the .dll file is not in my Windows XP SP3 (VirtualBox) that's why I wanted to use GDI+. Anyway, thank you for your help, and btw, I was so angry at the moment when I started this thread, I'm not usually like this

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    Thank you so much Bonnie. The first link helped me, but I think I am calling the function in a wrong way:

    Code:
    Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
    Private Declare Function GdipLoadImageFromFile Lib "GdiPlus.dll" (ByVal mFilename As Long, ByRef mImage As Long) As Long
    Private Declare Function GdipDeleteGraphics Lib "GdiPlus.dll" (ByVal mGraphics As Long) As Long
    Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, hGraphics As Long) As Long
    Private Declare Function GdipDrawImage Lib "GdiPlus.dll" (ByVal mGraphics As Long, ByVal mImage As Long, ByVal mX As Single, ByVal mY As Single) As Long
    Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
    Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
    Private Type GdiplusStartupInput
        GdiplusVersion           As Long
        DebugEventCallback       As Long
        SuppressBackgroundThread As Long
        SuppressExternalCodecs   As Long
    End Type
    
    Private Function RenderPNG(FileName As String, hDC As Long, X As Long, Y As Long) As Boolean
        On Error Resume Next
        Dim GDIsi As GdiplusStartupInput, gToken As Long, hGraphics As Long, hBitmap As Long
        GDIsi.GdiplusVersion = 1&
        GdiplusStartup gToken, GDIsi
        If Err Then
              Err.Clear
              Exit Function
        ElseIf gToken = 0& Then
             Exit Function
        End If
        On Error GoTo 0
        Call GdipCreateFromHDC(hDC, hGraphics)
        If hGraphics Then
            Call GdipLoadImageFromFile(StrPtr(FileName), hBitmap)
            If hBitmap Then
                GdipDrawImage hGraphics, hBitmap, X, Y
                GdipDisposeImage hBitmap
                RenderPNG = True
            End If
            GdipDeleteGraphics hGraphics
        End If
        GdiplusShutdown gToken
        
    End Function
    
    Private Sub Command_Click()
    If RenderPNG("E:\Documents and Settings\Kevin\Desktop\Notification Center\images\Battery\BatteryBG_8.png", Picture.hDC, Picture.ScaleX,Picture.ScaleY, 0) = False Then
    MsgBox "error"
    End If
    Picture.Refresh
    End Sub
    But it says "Run-time error 438: Object doesn't support this method or property"
    I set the picture's autoredraw to true in the IDE
    Any suggestions?
    Thanks

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Urrgh! Someone put an end to my misery please?

    Is your PictureBox named Picture? Note that the Form also has a property called Picture and you don't have to qualify it with the Form's name in order to access that property. You had an extra parameter as well (0). The ScaleX and ScaleY methods requires at least one argument but you didn't provide any. I think the X and Y arguments of the RenderPNG function are the client coordinates (in pixels) where the image starts to be rendered.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by Bonnie West View Post
    Is your PictureBox named Picture? Note that the Form also has a property called Picture and you don't have to qualify it with the Form's name in order to access that property. You had an extra parameter as well (0). The ScaleX and ScaleY methods requires at least one argument but you didn't provide any. I think the X and Y arguments of the RenderPNG function are the client coordinates (in pixels) where the image starts to be rendered.
    Yes my picturebox is named picture. But I'm gonna change it to pic1. Oops, that extra parameter (0) was accidental when I pasted the code in here and then idk what happened.What about this one:

    Code:
    Dim myPath As String
    myPath = "..."
    If RenderPNG(myPath, pic1.hDC, ScaleX(pic1.Width, pic1.ScaleMode, vbPixels), ScaleY(pic1.Height, pic1.ScaleMode, vbPixels)) = False Then
    MsgBox "error"
    End If
    pic1.Refresh
    This doesn't pop up any errors but it doesn't load the file

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,126

    Re: Urrgh! Someone put an end to my misery please?

    I didn't want to use WIA because for some strange reason the .dll file is not in my Windows XP SP3 (VirtualBox) that's why I wanted to use GDI+.
    I believe (if interested in WIA) you can download what you need HERE. BUT, only if interested in trying the WIA method.

  10. #10
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by kevinKZzaka View Post
    What about this one:

    . . .

    This doesn't pop up any errors but it doesn't load the file
    You're telling the RenderPNG function to draw your picture starting at the lower right corner of your PictureBox, hence you cannot see it. Try (..., 0&, 0&) instead.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by SamOscarBrown View Post
    I believe (if interested in WIA) you can download what you need HERE. BUT, only if interested in trying the WIA method.
    I am interested and I want to try it. But the problem is that I want my app to be a standalone, so that it wouldn't require any extra .dll-s because, maybe, those dll-s for some strange reason are not in the end-user's system. But I am more interested in GDI+ because this project of mine consists of using some images in order to create a certain effect and feel and look of the app (I am trying to mimic the iOS look) and instead of using a lot of images (for example to create an overlay or sth else, it's hard to explain) I could use GDI+, but this only after I have finished the first version of the application. Thanks

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by Bonnie West View Post
    You're telling the RenderPNG function to draw your picture starting at the lower right corner of your PictureBox, hence you cannot see it. Try (..., 0&, 0&) instead.
    Damn, that was so stupid of me
    Thank you so much for everything. You helped me a lot. The good thing here is that it also draws the png image on the form by just simply changing pic1.hdc to me.hdc

    I was thinking on skining the form around the image and if I have any problems, I will post it here.

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by kevinKZzaka View Post
    I am interested and I want to try it. But the problem is that I want my app to be a standalone, so that it wouldn't require any extra .dll-s because, maybe, those dll-s for some strange reason are not in the end-user's system. But I am more interested in GDI+ because this project of mine consists of using some images in order to create a certain effect and feel and look of the app (I am trying to mimic the iOS look) and instead of using a lot of images (for example to create an overlay or sth else, it's hard to explain) I could use GDI+, but this only after I have finished the first version of the application. Thanks
    Er, well...

    First of all WIA 2.0 always needs to be installed into Windows XP, which must be at SP 1 or later. However Microsoft pulled the WIA download because XP is considered "dead" and extended support is coming to an end. Lots of these add-ons are gone now.

    Somebody posted a link to download it, but I doubt Microsoft gave them permission to host it. So that would make it a pirated copy.

    GDI+ has some of the same issues, mainly that it isn't present in older Windows versions. Even XP SP2 systems may not have it. Unlike WIA 2.0 the download is still available for the redist package. Some people have the idea that GDI+ is shipped with Windows but that wasn't true until XP SP3 or so. Some do have it because an Office install or something else installed it.

    A security update added a feature to GDI+ that allows blocking various formats, for instance PNG. This is a system-wide setting. So even if a system has GDI+ it might not load PNG images.


    Basically, XP is on its very last legs.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    I don't care if XP is going to die. It's much better if it does. Vista is almost dead too, so the only left possibilities for an OS are Windows 7 and 8 which are two great OS. In this way, the XP and Vista users will migrate to 7 & 8 and the world will be a better place

    @Bonnie, I tried shaping the form to the picturebox in which the png image is drawn but it doesn't look good, it's a bit scrambled and doesn't shape perfectly. I don't know whether it's possible to shape a form to a png but I think it is according to an app I saw here: http://leandroascierto.com/blog/estado-del-tiempo/
    It's in spanish btw. Just look at a snapshot of the app. You see how there is the moon and the sun? They are out of the form and they are shaped. Perhaps those sun and moon images are .gif because I've tried form shaping with a gif and works perfectly. But the question here is: is it possible to shape a form to a draw png image?

    Or is there any other way that I can load my png picture and that there would be no picturebox background,like it was transparent?

    Sorry for no editing but it is currently impossibe to edit this text right now. Sorry

  15. #15
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by kevinKZzaka View Post
    But the question here is: is it possible to shape a form to a draw png image?

    Or is there any other way that I can load my png picture and that there would be no picturebox background,like it was transparent?
    A simple way would be to call the SetLayeredWindowAttributes API function. Rather than using the PNG image's Alpha channel, you'll have to choose instead which particular color in the picture that you want to become transparent.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Urrgh! Someone put an end to my misery please?

    Quote Originally Posted by Bonnie West View Post
    A simple way would be to call the SetLayeredWindowAttributes API function. Rather than using the PNG image's Alpha channel, you'll have to choose instead which particular color in the picture that you want to become transparent.
    Thank you Bonnie but already tried that but I think it's the png's fault probably because it has shadows and other effects (using Photoshop). I tried removing the shadows and more but still no luck. I then tried regioning to a picturebox and I still think is the image's fault so my simple solution to this was:

    On my form I have a background picture. And I then put a picturebox. I set the picturebox's left to a fixed value and then calculated this on Photoshop. So what I did was cut the part that the picturebox would take, and created some images on PS so when I load the picture it will look as if it's transparent but it just has the same background as the part that it takes. Idk if you get it, it's a bit hard to explain. Basically, just a lot of PS work, and I will make about 24 images (JPG) and use an imagelistbox.


    Thank you so much for your help. Am marking this thread as resolved

Tags for this Thread

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