Hi
I am getting error Incorrect Image on - .PaintPicture pctSource, 0, 0, w, h, X, Y, X + w, Y + h
VB.NET
CropAndSave Picture1, 500, 500, 1000, 1000, "C:\image.jpg"
Public Sub CropAndSave(pctSource As PictureBox, _
X As Integer, Y As Integer, _
h As Integer, w As Integer, _
sFile As String)
'=====================================================
Dim pct As PictureBox
Set pct = Controls.Add("VB.Picturebox", "pctTemp")
'---------------------------------------------------------------
'TESTING ONLY
'pct.Move pctSource.Left + pctSource.Width, pctSource.Top, w, h
'pct.Visible = True
'---------------------------------------------------------------
With pct
.Width = w
.Height = h
.AutoRedraw = True
.AutoSize = True
.PaintPicture pctSource, 0, 0, w, h, X, Y, X + w, Y + h
.Refresh
SavePicture pct.Image, sFile
End With
Controls.Remove "pctTemp"
Set pct = Nothing
End SubThanks

Sophia CarterRank #4,018 · 371 reputationPosted Mar 18, 2025, 8:00 AM EDT
It seems like you're encountering an issue with the `.PaintPicture` method in your VBA code. The error message "Incorrect Image" could suggest a problem with the source image or how it is being handled in the `CropAndSave` subroutine.
One possible reason for this error could be that the `pctSource` PictureBox object might not have a valid image or the image dimensions may not align with the parameters passed to the `.PaintPicture` method. It's essential to ensure that `pctSource` contains a valid image before attempting to paint it onto another PictureBox.
Additionally, when using the `.PaintPicture` method, make sure that the source image dimensions (width and height) match the destination PictureBox (`pct`) dimensions specified by `w` and `h`. Any size discrepancies could lead to an "Incorrect Image" error.
To troubleshoot this issue, you might consider adding validation checks to ensure that `pctSource` is not null and that its image dimensions are suitable for painting onto the temporary PictureBox `pct`.
Here's a basic example of how you could enhance your code with some validation checks:
Public Sub CropAndSave(pctSource As PictureBox, _ X As Integer, Y As Integer, _ h As Integer, w As Integer, _ sFile As String) If Not pctSource.Picture Is Nothing And pctSource.Picture.Width > 0 And pctSource.Picture.Height > 0 Then ' Proceed with cropping and saving logic ' Ensure the source image dimensions align with the PictureBox dimensions Else MsgBox "Error: Invalid source image in pctSource" End If End SubBy incorporating such checks and ensuring image dimension compatibility, you can prevent the "Incorrect Image" error and handle scenarios where the source image may not be suitable for painting.