Solved

GGGGGRRRRRR Bitmap help please

Posted on 2003-11-30
10
241 Views
Last Modified: 2010-05-01
I am trying to edit a bitmap image from Microsoft Access.  This code does not work

Sub SavePic()

    Dim Pic As New StdPicture
    Dim A As Long
    Dim B As Long

    Set Pic = LoadPicture("C:\Dot.bmp")
    A = SetPixel(Pic.hdc, 0, 0, RGB(255, 0, 0))
   
    SavePicture Pic, "C:\Dot.bmp"
    MsgBox A
   
End Sub

msgbox A always returns -1 and i cant figure out what "hdc" is or how to get this done.

500 points to the one who can fix this fast.


0
Comment
Question by:OptionsMM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:sanjaykashyap19
ID: 9847213
I not have idea about Access
If AutoRedraw property is there then set the
Pic.AutoRedraw = True

look

http://oldlook.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20698681.html


Following code working fine in VB

' Randomly color all the pixels in window Form1.  This example
' gets the rectangle of Form1 and iterates through all the points
' (pixels) inside of it.
Dim winrect As RECT  ' rectangle of window Form1
Dim rgbval As Long  ' RGB value of the randomly selected color
Dim x As Long, y As Long  ' counters for x and y coordinates
Dim retval As Long  ' return value

' Get the rectangle of window Form1.
retval = GetWindowRect(Form1.hWnd, winrect)

' Loop through each pixel within Form1.
For y = 0 To winrect.bottom - winrect.top
  For x = 0 To winrect.right - winrect.left
    ' Select a random color by choosing a value between 0 and 255
    ' inclusive for each component of the color.
    rgbval = RGB(Int(256 * Rnd), Int(256 * Rnd), Int(256 * Rnd))
    ' Set the pixel to the color above.
    retval = SetPixel(Form1.hDC, x, y, rgbval)
  Next x
Next y

0
 

Author Comment

by:OptionsMM
ID: 9847240
This picture is not embedded in a form.  How do I adjust it withou having a .hDC reference to something in a form?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9848155
OptionsMM,

hDC stands for "Handle to Device Context".  You can only have a Device Context when you have a graphics object associated with some kind of device, such as the screen or printer.

As far as I know, you cannot manipulate an image without having it associated with some kind of control, be it either a form, picturebox or image control.  The control need not be visible though.

The StdPicture object gives you a way to store references to images in memory and easily switch the image referenced by a Device Context to a different one.

Regards,

Idle_Mind
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:OptionsMM
ID: 9848173
Okay- now we are getting somewhere.

Lets say I wanted to write a piece of code to create a new bitmap with 2 pixels to do the following

set 0, 0 to vbred
set 0, 1 to vb blue

and the save it to the C drive as "Dot".

If I could get this code I would be set :)

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9848227
You would do it basically as you have.  Add a PictureBox, "Picture1",  to your Form.

There are two versions of the code below.  

The first version will modify the two pixels in an existing image and overwrite it.

The second version will create a new image with just the two pixels.

Regards,

Idle_Mind

Sub SavePic() ' Modify an existing image "C:\Dot.bmp"
    Dim A As Long
    Dim B As Long

    Set Picture1.Picture = LoadPicture("C:\Dot.bmp")
    A = SetPixel(Picture1.hdc, 0, 0, RGB(255, 0, 0))
    B = SetPixel(Picture1.hdc, 0, 1, RGB(0, 0, 255))

    Set Picture1.Picture = Picture1.Image
    SavePicture Picture1.Picture, "C:\Dot.bmp"    
End Sub

Sub SavePic() ' Create a new image "C:\Dot.bmp"
    Dim A As Long
    Dim B As Long

    Me.ScaleMode = vbPixels
    Picture1.Width = 1
    Picture1.Height = 2
   
    A = SetPixel(Picture1.hDC, 0, 0, RGB(255, 0, 0))
    B = SetPixel(Picture1.hDC, 0, 1, RGB(0, 0, 255))

    Set Picture1.Picture = Picture1.Image
    SavePicture Picture1.Picture, App.Path & "\Dot.bmp"
End Sub
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 9849898
It'll be alot faster keeping your image in memory for modifying the bitmap data than just using SetPixel on a device context. If you aren't going to modify alot, go with Idle's choice. If you are, invest time to increase performance alot. Would write an example for what you want but might not be required since there are alot of examples of it already

"How to use Direct Memory Access to do Graphics"
http://www.visualbasicforum.com/t25347.html


If you need further help on the above example just ask and i'll come up with my own.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 9849906
As a side not, i'm foggy in the area of you saying you're using MS Access, so if you have strict limitations with using certain Win32 API or general memory, then do say so.
0
 

Author Comment

by:OptionsMM
ID: 9853026
Yeah it isnt working.  

I cant seem to get the .hdc to work

I am using an "Image", and trying it with an OLEUnbound.  still i cant seem to get the .mdc property to work

is this the same as a Picturebox in VB?
is there a reference i need to add in?

Sub SavePic()

    Dim Image As Image
    Dim A As Long
    Dim B As Long
     
    Set Image = Forms!Main.Image
    Image.Picture = "C:\Clean.bmp"
    MsgBox Image.hdc
   
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9853439
Hmm....seems the intrinsic controls in Access do not expose a Device Context...and a VB PictureBox is not the same as an Access Image Control.

I have seen some posts on how to get a PictureBox into Access and the short answer is you don't want to go there.
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20508015.html?query=ms+access+picturebox+hdc&searchType=topic

Trying to find how to get an hDC for an Access Image Control...

Idle_Mind
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 9853490
Here is a control you could try:

http://www.lebans.com/imageclass.htm

Idle_Mind
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 13 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question