I have drawn an image using SetPixelV, unfortunately, it does not do anything to update the .image property, so vb's built in SaveImage method only saves a black picture. Any help? Thanks!
Printable View
I have drawn an image using SetPixelV, unfortunately, it does not do anything to update the .image property, so vb's built in SaveImage method only saves a black picture. Any help? Thanks!
Make sure you have set the Picture Box's Autoredraw property to True and also make sure you call the Picture Box's Refresh method before using SavePicture, i.e. ....
Code:
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Form_Load()
Picture1.AutoRedraw = True
Picture1.ScaleMode = vbPixels
End Sub
Private Sub Picture1_Click()
For I = 1 To 1000
Randomize
SetPixelV Picture1.hdc, Rnd * Picture1.ScaleWidth, Rnd * Picture1.ScaleHeight, RGB(Rnd * 255, Rnd * 255, Rnd * 255)
Next I
Picture1.Refresh
SavePicture Picture1.Image, "C:\Pixels.BMP"
End Sub
Unfortunately, this does not work. SetPixel's results are erased by refresh and AutoRedraw. I need some way to edit the image property itself, or some other solution.
The code I posted works just fine for me.
You must set the AutoRedraw before you draw anything and Refresh after the job is coplete.
Correct me if I'm wrong, but it sounds to me that you've set AutoRedraw and Refresh both after?
Works fine for me too.
Make sure these are your steps:
1. Set AutoRedraw to True
2. Set pixels with SetPixelV
3. Refresh the picture with .refresh
4. Save image using built-in code.
Cheers,
Sastraxi