Home > Software engineering >  Dry! With StretchBlt image processing, save images have black side?
Dry! With StretchBlt image processing, save images have black side?

Time:09-19

With StretchBlt image has a black border after processing,
The original image is as follows:

To save the pictures after processing as follows (processed images more than 2 m BBS can't upload, so the images for the QQ screenshots, display effect is as follows) :




The code is as follows:
Idea: to make longer than wide, 102/152 of the image size, picture2 control loading the original images, to obtain the original image width pixels, the original pixel values calculated according to the ratio of 102/152, to calculate the length or width on the size of the need to supplement, in order to achieve the ratio 102/152, then put the picture1 controls the width size set by computing the target size, and then use StretchBlt function will play picture1 picture2 control images load control, and then save the picture1 control target image, is saved to disk C of 123. The JPG as source code, it is strange that saved pictures, there are black border, is really don't know how to deal with, consult everybody!


 'Form1 to add a picture box picture1 
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
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 Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hDC As Long, ByVal nStretchMode As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWND) As Long As Long
Private Const HALFTONE=4
Private Const SRCCOPY=& amp; HCC0020

Dim the fs
Dim wScreen As Long
Dim hScreen As Long
Dim w As Long
Dim h As Long
Dim the cw As Long
Dim ch As Long
Dim the Dw As Long
Dim Dh As Long
Dim DifferenceH As Long
Dim DifferenceW As Long
Dim PIC As PictureBox
Dim RTN As Boolean
Dim RTM As Long
Dim Hdc1 As Long, Hdc2 As Long

Private Sub Form_Load ()
Picture2. Picture=LoadPicture (" C: \ 1. JPG ")
Printer. ScaleMode=3
Printer. ScaleWidth=1181
Printer. ScaleHeight=1772
Printer. ScaleLeft=0
Printer. ScaleTop=0
AllPrint


End Sub
Sub AllPrint () 'this function is used to cut printing images

W=Picture2. Width
H=Picture2. Height

If w & lt;=h Then 'this judgment to broadband is less than and so on the height of the printed picture
Printer. The Orientation=1 'Printer longitudinal
Ch=w * 152\102
The cw=102 * h \ 152
DifferenceW=w - the cw
DifferenceH=h - ch
The Debug. Print DifferenceW
The Debug. Print DifferenceH
If DifferenceW & gt;=0 And DifferenceH & lt;=0 Then
Picture1.
Width=wPicture1. Height=ch
Call SetStretchBltMode (Picture1. HDC, HALFTONE)
RTM=StretchBlt (Picture1. HDC, 0, Abs (DifferenceH/2), w, ch, Picture2. HDC, 0, 0, w, ch, vbSrcCopy)
Picture1. Refresh
SavePicture Picture1. Image, "c: \ 123 JPG"
End the If
End the If
End Sub

CodePudding user response:

Suggest the original poster or a PictureBox PaintPicture method to deal with,

Refer to the following code, but you should pay attention to is,
In the design window, several attributes in the following set of PictureBox (in the Form_Load () to add the code can also be used) :
Picture1. Appearance=0
Picture1. BorderStyle=0
Picture1. AutoRedraw=True

Picture2. AutoSize=True

Zooming code:
 Sub AllPrint () 
W=Picture2. ScaleWidth
H=Picture2. ScaleHeight
If w & lt;=h Then
Ch=152/102 w *
The cw 102/152=h *
'this is the original height, width adjust proportion
Picture1. Width=the cw
Picture1. Height h=
Picture1. PaintPicture Picture2. Image, 0, 0, the cw, h, 0, 0, w, h, vbSrcCopy

'this is the width of the constant, adjust the height to fit proportion
'Picture1.
Width=w'Picture1. Height=ch
'Picture1. PaintPicture Picture2. Image, 0, 0, w, ch, 0, 0, w, h, vbSrcCopy
SavePicture Picture1. Image, "E: \ Temp \ 123. BMP"
End the If
End Sub

SavePicture saved is Bmp format,
You write extension. The JPG is not practical, if you want to save JPG format image files, can search on the Internet code,

CodePudding user response:

By using API function PictureBox HDC operations,
If the PictureBox is obscure (all, or part of), will often appear similar to the result of the "flower screen",

CodePudding user response:

Thank Chen8013 reply, may be I express problem, now and then I put my saying:

Cutting pictures to meet the proportion, this I have solved, but the problem is that the cutting will lead to image information loss,

So going to compensation method is adopted to realize now, so we would not lead to image information is cut off,

Is to use the pixel size of the original image is calculated according to certain proportion, and calculated the length direction or width need to compensate the pixel width,

Such as 1/f, I post the second picture, after the treatment, is the need to compensate the size in the height direction, so the picture above and should have white space, the issuance of

But I now is processing the white space above normal, they became black below, compensation are white (top and bottom will meet demand)

Want to master to appreciate your guidance!

CodePudding user response:

 Sub x () 
'assume that original image 1000 * 1000'
W=1000
H=1000

Ch=w * 152\102
The cw=102 * h \ 152

DifferenceW=w - the cw
DifferenceH=h - ch

'StretchBlt (Picture1. HDC, 0, Abs (DifferenceH/2), w, ch, Picture2. HDC, 0, 0, w, ch, vbSrcCopy)'
The Debug. Print "target", 0, Abs (DifferenceH/2), w, ch
The Debug. Print "source", 0, 0, w, ch
End Sub

 target 0, 245, 1000, 1490 
Source 0 0 1000 1490

You cut 1000 from the 1000 * 1000 * 1490, what is the content below 490?nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • VBA
  • Related