Results 1 to 18 of 18

Thread: Enlarge/Compress Bitmap

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Lightbulb

    I have a bitmap (I don't know the dimension) and I need to put it into my VB program (either PictureBox or Image is OK)

    I try to use the Stretch, but it stretch my picture box, instead of expand or compress my bitmap image.

    How can I expand or compress my bitmap?



  2. #2
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    well, to start, you can find the dimensions of your bmp by opening it in MS Paint and choosing IMAGE >> ATTRIBUTES.

    When you get the pixels , multiply by 15 and that will be the twip size.

    For example an image 200x300 pixels has twip size 3000x4500.

    To put the image in your program you can either set an image or picture box to those dimensions and then insert the picture, or you can put an IMAGE box on the form and set STRETCH to FALSE. WHen you assign the image, that will make the image box exactly the size of your bitmap.


    NOW, if you wanted to have the same picture on your form in the same proportions, but smaller or larger, let me know, because I just wrote a small piece of code like that for a screensaver.


    Good luck.

  3. #3
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    well, what the heck.

    this is what i came up with

    ==============================

    strPicFile = (the path to your pic)
    Image2.Picture = LoadPicture(strPicFile)
    If Image2.Width > Image2.Height Then
    Image1.Width = 3000
    Image1.Height = (Image1.Width / Image2.Width) * Image2.Height
    Image1.Picture = Image2.Picture
    End If
    If Image2.Width <= Image2.Height Then
    Image1.Height = 3000
    Image1.Width = (Image1.Height / Image2.Height) * Image2.Width
    Image1.Picture = Image2.Picture
    End If
    =======================================


    In this case, image2 has visible set to FALSE and stretch set to false

    image1 has Visible set to TRUE and stretch set to true


    When you run it, the pic will appear in correct dimensions on full screen. to change from full screen to set dimensions, just change my Screen.Width or Screen.Height to set numbers.

    Okay, again, good luck.



  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Exclamation

    Thanks Wengang,

    First, I am not going to change the size of my picture box / image box

    Second, I miss out the fact that the .bmp is created during runtime of my program, so I cannot go to MSPaint

    Third, the size of the picture box is fixed but the .bmp is variant

    Actually, I am going to print various bitmaps (I don't know the size) to printer, so I must limit the output dimension, and enlarge/compress (proportionally) the bitmap file.




  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You could use painpicture to stretch the pictures, in pictureboxes or forms, check out the vbhelp for usage, or search for paintpicture in this forum
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Talking

    Thanks Kedaman,

    I follow your idea by:

    1. create a picture box sp to store the original picture
    2. create a picture box np with my desired dimension

    3. Use np.PaintPicture sp.Picture, 1, 1, np.Width, np.Height

    I can get the picture in sp, to compress or expand into np


    One further question, when I try to use
    SavePicture np.Picture, "c:\temp\anything.bmp"

    It comes out error message telling me that the value is wrong

    If I run in IDE, np.Picture = 0

    Could you help?


  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    That is because you haven't loaded a picture in the PictureBox you have drawn the picture.
    To get the drawn picture use the Image property instead of the Picture property.
    Code:
    SavePicture np.Image, "c:\temp\anything.bmp"
    Good luck!

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Or you could set the image to the picture
    Code:
    Set np.Picture = np.Image
    Or you could set autoredraw to true
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by kedaman
    Or you could set autoredraw to true
    ?????????????
    Setting AutoRedraw to True will not persist the image. It will only redraw the image when needed.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Youre right Joacim, that's if you have a picture from the beginning, which apparently isn't the case
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Guest
    Put the following into a Form with a 2 PictureBoxes and a CommandButton. Make sure there is a picture loaded in Picture2.

    Code:
    Private Sub Command1_Click()
        SavePicture Picture1.Image, "C:\Windows\Desktop\MyFile.bmp"
    End Sub
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        'Make the Picture twice as large
        If KeyCode = vbKeyAdd Then
            With Picture1
                .Cls
                .Width = .Width * 2
                .Height = .Height * 2
                .PaintPicture Picture2.Picture, 0, 0, .ScaleWidth, .ScaleHeight
            End With
        End If
    
        'Make the Picture twice as small
        If KeyCode = vbKeySubtract Then
            With Picture1
                .Cls
                .Width = .Width / 2
                .Height = .Height / 2
                .PaintPicture Picture2.Picture, 0, 0, .ScaleWidth, .ScaleHeight
            End With
        End If
    
        'Center the PictureBox
        Picture1.Move (Me.ScaleWidth / 2) - (Picture1.Width / 2), (Me.ScaleHeight / 2) - (Picture1.Width / 2)
    End Sub
    
    Private Sub Form_Load()
        Picture2.Visible = False
        Me.KeyPreview = True
        Picture1.AutoRedraw = True
        Picture1.PaintPicture Picture2.Picture, 0, 0
    End Sub
    When you press + (Add), the Bitmap will grow twice it's size and when you press - (Subtract), the bitmap will shrink to half it's amount.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Unhappy

    kedaman, I tried the code and it works....

    but, when I save the np.picture, it saves with the original dimension, not the one that I see on the screen (that has been compressed or enlarged).

    How I can save the enlarged image?


    Secondly, I tried to draw a button on the picture box (ie the picturebox as the parent), how I can save the 'look' of the picture box to a bmp, including the button?

    Can anybody help?


  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Savepicture should save the image with the dimensions your picturebox or form have, so you should resize it before saving.
    To capture the button too it won't work with savepicture because the button isn't included in the picture. You could use Bitblt api(searh for it to find the declaration) Then you set autoredraw to false, blit the picture to another picture which autoredraw should be set to true, and save that image.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Question

    kedaman,

    I already can see the compressed picture in the picture box, (I use a wrong ratio so that the picture will look very different)

    and then I use a command button to save the picture.

    But if I open the bmp file using MSPaint, the ratio is like as if it is not compressed.



  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's because the picture doesn't match the image. did you do this?
    Code:
    SavePicture np.Image, "c:\temp\anything.bmp"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Unhappy

    I tried various way and the results are as follow:

    (SrcPic already Set Picture property, while NewPic has Picture property = None)

    Case 1:

    NewPic.PaintPicture SrcPic.Picture, 1, 1, NewPic.ScaleWidth, NewPic.ScaleHeight

    SavePicture NewPic.Picture, "c:\xxnew.bmp"

    ==> Error for second statement

    Case 2:

    NewPic.PaintPicture SrcPic.Picture, 1, 1, NewPic.ScaleWidth, NewPic.ScaleHeight

    SavePicture NewPic.Image, "c:\xxnew.bmp"

    ==> I can see the compressed picture in NewPic, but the saved bmp only has a grey background and nothing on it

    Case 3:

    Set NewPic.Picture = SrcPic.Picture
    NewPic.PaintPicture SrcPic.Picture, 1, 1, NewPic.ScaleWidth, NewPic.ScaleHeight

    SavePicture NewPic.Picture, "c:\xxnew.bmp"

    ==> The saved bmp file is exactly the same as SrcPic.Picture (without any compression)


    Could you help?


  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    for 1: 
      put
      newpicture.picture=newpicture.image
      between the lines
    
    for 2:
      set autoredraw to true on newpicture
    
    for 3:
      save the image instead of the picture
    but well you could choose one of them
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Talking

    I use the second method, and it works now.

    THANK YOU KEDAMAN


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