Consulting

Results 1 to 8 of 8

Thread: Solved: Bitmaps with Excel VBA, how to create

  1. #1
    VBAX Regular
    Joined
    Nov 2006
    Posts
    8
    Location

    Solved: Bitmaps with Excel VBA, how to create

    Hi folks,

    How does one create a bitmap file with Excel VBA?

    All I need is the very basics, like how to create a file that is, for example, 100 pixels tall and 200 pixels wide, with a blue triangle and white background, or anything remotely like that.

    Thanks, yet again, in advance.

    - Ray

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Do you need the code, or just the functionality? I haven't tried this myself, but you may want to look at this:

    "Create Bitmap from Selection"
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi Ken,

    I tried it and my opinion is; this add-in is very useful.

    Thank you very much.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  4. #4
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Also, Excel (well xl2000+ anyway) has a built-in camera function, here's some very old code I have that gives an example usage for you to play with... [vba]
    Option Explicit
    '
    Sub DoCamera()
    '
    Dim CaptureRange As Range
    Dim OutputRange As Range
    '
    'Prompt user for range to capture
    On Error Resume Next
    Set CaptureRange = Application.InputBox(Prompt:= _
    "Select the range you would like to capture.", _
    Title:="User Input Required", _
    Default:=ActiveCell.Address, Type:=8)
    If CaptureRange Is Nothing Then End
    On Error GoTo 0
    '
    'Copy range to Clipboard as picture
    CaptureRange.CopyPicture xlScreen, xlBitmap
    '
    'Prompt user for range to paste to
    On Error Resume Next
    Set OutputRange = Application.InputBox(Prompt:= _
    "Select the range on which you would like to paste.", _
    Title:="User Input Required", _
    Default:=ActiveCell.Address, Type:=8)
    If OutputRange Is Nothing Then End
    '
    'Paste picture to output range
    OutputRange.PasteSpecial
    Selection.Formula = CaptureRange.Address
    ActiveSheet.Shapes(1).Select
    With Selection.ShapeRange
    .ScaleWidth 1.5, msoFalse, msoScaleFromTopLeft
    .ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
    '.Cut
    End With
    End Sub

    [/vba]

    (edited)
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Here's some more ideas you can play with...

    The picture tool can be found in View > Toolbars > customize > tools (it has a picture of a camera on it) - drag it and put it on a toolbar somewhere.

    Now (you can record all this if you like)
    Select a range - the range can include shapes, pictures (whatever) and click the camera tool. Your mouse icon is now a cross, put the cross anywhere on the sheet (or another sheet {this can also be in another workbook}) left-click and drag the mouse icon. You'll now see a picture there, and in the formula bar you'll see the address of the selected range - this is because the picture is now linked to your original range. Any changes you now make (including any changes to shapes, cell formatting, etc) in the original range will show in this linked picture, and, if you change the range link in the formula bar, you'll get a new picture of this other range. This allows you create a "window" (or any number of them) that allows you to view changes on other worksheets when changes are made to the active sheet.

    Another 'bonus' is that double-clicking the picture will activate the sheet and select the range it's linked to (and if the picture is linked to a closed book it will open that book)

    To make an unlinked picture - select a range as before, hold down Shift, go to Edit and select Copy Picture - you'll get a dialog box offering a choice of Picture or Bitmap - press OK and let go of Shift. Select the range where you want your picture, hold down Shift, go to Edit again and select Paste Picture (n.b. those options are only available while holding down Shift).

    Note that you can also format the picture if you like...

    Have a read here to read about some practical uses for linked pictures.

    HTH

    (edited)
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Sound slike the guy wants Excel to be a paint type program.

  7. #7
    VBAX Newbie
    Joined
    Jan 2012
    Posts
    1
    Location
    I see that this is 'solved', but perhaps someone will be interested in a more versatile solution.
    See my blog.
    zababcdblog.blogspot.com/2012/01/how-to-write-bmp-file-from-excel-vba.html
    And
    zababcdblog.blogspot.com/2012/01/how-to-write-bmp-file-from-excel-vba-2.html

    It's actually very simple to write it directly as bytes, once you know the bitmap file structure. The second link is a slight improvement on the first so that you don't need to read from a file.

    The blue triangle on a white background (for example), would be written like this:
    For x = 1 To ImgHeight
    For y = 1 To ImgWidth
    If y <= (ImgWidth + 1)/2 Then 'Left half of the picture,
    'including middle column if any
    If x >= 2*y/(ImgWidth + 1) Then 'If the row is below or at
    'the percentage of height that the column is of halfway
    ImageColours(x, y, 1) = 0 'no red
    ImageColours(x, y, 2) = 0 'no green
    ImageColours(x, y, 3) = 255 'all blue
    Else
    For z = 1 to 3
    ImageColours(x, y, z) = 255 'all colours = white
    Next z
    End If
    Else 'if it's on the right side of the image, then the left
    For z = 1 to 3 'is already done so just reflect the image
    ImageColours(x, y, z) = ImageColours(x, ImgWidth - y + 1, z)
    Next z
    End If
    Next y
    Next x

  8. #8
    VBAX Newbie
    Joined
    Jan 2012
    Posts
    5
    Location
    I'm glad to learn of an elaborate solution to this problem.

Posting Permissions

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