When saving images in AVI, there are several negative aspects. Firstly, the quality is disgusting even if you save without compression.

Secondly, sometimes it is saved in such a way that it is impossible to open the AVI file later.
And if you set the resolution to 110x300 and save in Microsoft Video 1 format, then the image seems to shift.

But if set resolution to 320x240 and save in Microsoft Video 1 format, then the image seems ok!

Maybe the reason is the resolution? And in AVI can't be used any resolution?
Here is a code example:
Code: Select all
;d'un code écrit en c++ par son auteur Chiew Heng Wah
;fixed by ADAmor Ziltch 2014
;https://www.purebasic.fr/english/viewtopic.php?p=453731#p453731
EnableExplicit
#CAPTUREBLT = $40000000
#streamtypeAUDIO = $73647561
#streamtypeVIDEO = 1935960438
#ICMF_CHOOSE_KEYFRAME = 1
#ICMF_CHOOSE_DATARATE = 2
#AVIERR_OK = 0
#AVIIF_KEYFRAME = $10
#BmpFile = 20
#AviFile = 30
#AVISTREAMINFO_DISABLED = $1
#AVISTREAMINFO_FORMATCHANGES = $10000
#AVISTREAMREAD_CONVENIENT = -1
#AVSTREAMMASTER_AUDIO = 0
#AVSTREAMMASTER_NONE = 1
#AVICOMPRESSF_DATARATE = $2
#AVICOMPRESSF_INTERLEAVE = $1
#AVICOMPRESSF_KEYFRAMES = $4
#AVICOMPRESSF_VALID = $8
Macro MAKE_SCODE(sev, fac, code)
((sev << 31) | (fac << 16) | (code))
EndMacro
#FACILITY_ITF = $0
Macro MAKE_AVIERR(e)
MAKE_SCODE(#SEVERITY_ERROR, #FACILITY_ITF, $4000 + e)
EndMacro
#AVIERR_OK = 0
#AVIERR_UNSUPPORTED = MAKE_AVIERR(101)
#AVIERR_BADFORMAT = MAKE_AVIERR(102)
#AVIERR_MEMORY = MAKE_AVIERR(103)
#AVIERR_INTERNAL = MAKE_AVIERR(104)
#AVIERR_BADFLAGS = MAKE_AVIERR(105)
#AVIERR_BADPARAM = MAKE_AVIERR(106)
#AVIERR_BADSIZE = MAKE_AVIERR(107)
#AVIERR_BADHANDLE = MAKE_AVIERR(108)
#AVIERR_FILEREAD = MAKE_AVIERR(109)
#AVIERR_FILEWRITE = MAKE_AVIERR(110)
#AVIERR_FILEOPEN = MAKE_AVIERR(111)
#AVIERR_COMPRESSOR = MAKE_AVIERR(112)
#AVIERR_NOCOMPRESSOR = MAKE_AVIERR(113)
#AVIERR_READONLY = MAKE_AVIERR(114)
#AVIERR_NODATA = MAKE_AVIERR(115)
#AVIERR_BUFFERTOOSMALL = MAKE_AVIERR(116)
#AVIERR_CANTCOMPRESS = MAKE_AVIERR(117)
#AVIERR_USERABORT = MAKE_AVIERR(198)
#AVIERR_ERROR = MAKE_AVIERR(199)
Structure bitmap24
bmiHeader.BITMAPINFOHEADER
EndStructure
Structure msvc
a.b
b.b
c.b
d.b
EndStructure
Structure AVI_STREAM_INFO
fccType.l
fccHandler.msvc
dwFlags.l
dwCaps.l
wPriority.w
wLanguage.w
dwScale.l
dwRate.l
dwStart.l
dwLength.l
dwInitialFrames.l
dwSuggestedBufferSize.l
dwQuality.l
dwSampleSize.l
rcFrame.RECT
dwEditCount.l
dwFormatChangeCount.l
szName.a[64]
EndStructure
Structure AVI_COMPRESS_OPTIONS
fccType.l
fccHandler.l
dwKeyFrameEvery.l
dwQuality.l
dwBytesPerSecond.l
dwFlags.l
*lpFormat
cbFormat.l
*lpParms
cbParms.l
dwInterleaveEvery.l
EndStructure
Structure ImageStr
Filename$
*Buffer
EndStructure
Prototype AVIFileInit()
Prototype AVISaveOptions(iHwnd.i,iFlags.i,iNstreams.i,ptrAvi,iOpts.i)
Prototype AVISaveVA(sFileSave.s,iPclsidhandler.i,iLpfncallback,iNstreams.i,iPpavistream.i,iPpcompoptions.i)
Prototype AVIMakeCompressedStream(iPpsCompressed.i,iPsSource.i,LpOptions.i,iPclsidHandler.i)
Prototype.l AVIFileOpen(iPfile.l, sFileOpen.p-Ascii, iUmode.l, iPclsidHandler.l)
Prototype AVIFileGetStream(ipfileStream.i,ptr_avi,iFccType.i,iLParam.i)
Prototype AVIStreamStart(iPavi.i)
Prototype AVIStreamLength(iPavi.i)
Prototype AVIStreamInfo(iPaviStream.i,psi.i,iLsize.i)
Prototype AVIStreamGetFrameOpen(ptrAviStream,BitmapHeader)
Prototype AVIStreamGetFrame(iPGetFrameObj.i,iLpos.i)
Prototype AVIFileCreateStream(ptrAviFile,ptrAviStream,ptrStreamInfo)
Prototype AVIStreamSetFormat(ptrAviStream,iLpos.i,LpFormat,iCbFormat.i)
Prototype AVIStreamWrite(ptrAviStream,iLstart.i,iLsamples.i,ptrLpBuffer,iCbBuffer.i,iDwFlags.i,iDummy1.i,iDummy2.i)
Prototype AVIStreamGetFrameClose(iPGetFrameObj.i)
Prototype AVIStreamRelease(ptrAviStream)
Prototype AVIFileRelease(iPfile.i)
Prototype AVIFileExit()
Global bmi.bitmap24
Global NewList ImageList.ImageStr()
Global FrameRate
Procedure GetImages(x, y, w, h, frames)
Protected trgDC, hDC, bitBlt, *Buffer, PixelFormat, Pitch, dy
Protected startime
If IsImage(0)
FreeImage(0)
EndIf
CreateImage(0, w, h, 24)
If ListSize(ImageList())
ForEach ImageList()
If ImageList()\Buffer
FreeMemory(ImageList()\Buffer)
EndIf
Next
ClearList(ImageList())
EndIf
If Not trgDC
trgDC = GetDC_(GetDesktopWindow_())
EndIf
bmi\bmiHeader\biSize = SizeOf(bitmap24)
bmi\bmiHeader\biBitCount = 24
bmi\bmiHeader\biPlanes = 1
bmi\bmiHeader\biWidth = ImageWidth(0)
bmi\bmiHeader\biHeight = ImageHeight(0)
bmi\bmiHeader\biSizeImage = bmi\bmiHeader\biWidth * bmi\bmiHeader\biHeight * 3
While frames
startime = ElapsedMilliseconds()
hDC = StartDrawing(ImageOutput(0))
bitBlt = BitBlt_(hDC, 0, 0, w, h, trgDC, x, y, #SRCCOPY|#CAPTUREBLT)
*Buffer = DrawingBuffer()
PixelFormat = DrawingBufferPixelFormat()
Pitch = DrawingBufferPitch()
StopDrawing()
AddElement(ImageList())
ImageList()\Buffer = AllocateMemory(bmi\bmiHeader\biSizeImage)
If ImageList()\Buffer
For dy = 0 To bmi\bmiHeader\biHeight - 1
CopyMemory(*Buffer + (dy * Pitch), ImageList()\Buffer + (dy * bmi\bmiHeader\biWidth * 3), bmi\bmiHeader\biWidth * 3)
Next dy
EndIf
frames-1
If (ElapsedMilliseconds()-startime)<(1000/FrameRate)
Delay(1000/FrameRate - (ElapsedMilliseconds()-startime))
EndIf
Wend
If trgDC
ReleaseDC_(GetDesktopWindow_(),trgDC)
EndIf
EndProcedure
Procedure SaveAVI()
Protected sgPat.s, sgAviFile.s, ResultL.l, *pavfile.IAVIFILE, WindHwnd, pAVICompressed, igIndex.i = 0
Protected Opts.AVI_COMPRESS_OPTIONS
Protected pFile.IAVIFILE
Protected pStream
Protected strhdr.AVI_STREAM_INFO
Protected AviLib
Protected AVIFileInit.AVIFileInit
Protected AVISaveOptions.AVISaveOptions
Protected AVISaveVA.AVISaveVA
Protected AVIMakeCompressedStream.AVIMakeCompressedStream
Protected AVIFileOpen.AVIFileOpen
Protected AVIFileGetStream.AVIFileGetStream
Protected AVIStreamStart.AVIStreamStart
Protected AVIStreamLength.AVIStreamLength
Protected AVIStreamInfo.AVIStreamInfo
Protected AVIStreamGetFrameOpen.AVIStreamGetFrameOpen
Protected AVIStreamGetFrame.AVIStreamGetFrame
Protected AVIFileCreateStream.AVIFileCreateStream
Protected AVIStreamSetFormat.AVIStreamSetFormat
Protected AVIStreamWrite.AVIStreamWrite
Protected AVIStreamGetFrameClose.AVIStreamGetFrameClose
Protected AVIStreamRelease.AVIStreamRelease
Protected AVIFileRelease.AVIFileRelease
Protected AVIFileExit.AVIFileExit
If ListSize(ImageList())
AviLib = OpenLibrary(#PB_Any, "avifil32.dll")
If AviLib
AVIFileInit = GetFunction(AviLib, "AVIFileInit")
AVISaveOptions = GetFunction(AviLib, "AVISaveOptions")
AVISaveVA = GetFunction(AviLib, "AVISaveVA")
AVIMakeCompressedStream = GetFunction(AviLib, "AVIMakeCompressedStream")
AVIFileOpen = GetFunction(AviLib, "AVIFileOpen")
AVIFileGetStream = GetFunction(AviLib, "AVIFileGetStream")
AVIStreamStart = GetFunction(AviLib, "AVIStreamStart")
AVIStreamLength = GetFunction(AviLib, "AVIStreamLength")
AVIStreamInfo = GetFunction(AviLib, "AVIStreamInfo")
AVIStreamGetFrameOpen = GetFunction(AviLib, "AVIStreamGetFrameOpen")
AVIStreamGetFrame = GetFunction(AviLib, "AVIStreamGetFrame")
AVIFileCreateStream = GetFunction(AviLib, "AVIFileCreateStream")
AVIStreamSetFormat = GetFunction(AviLib, "AVIStreamSetFormat")
AVIStreamWrite = GetFunction(AviLib, "AVIStreamWrite")
AVIStreamGetFrameClose = GetFunction(AviLib, "AVIStreamGetFrameClose")
AVIStreamRelease = GetFunction(AviLib, "AVIStreamRelease")
AVIFileRelease = GetFunction(AviLib, "AVIFileRelease")
AVIFileExit = GetFunction(AviLib, "AVIFileExit")
AVIFileInit()
sgPat = "AVI Movie File (*.AVI *.avi)|*.AVI;*.avi"
sgAviFile = SaveFileRequester("Enter AVI File Name", "", sgPat, 0)
;sgAviFile = GetPathPart(ProgramFilename()) + "Test1.avi"
If(GetExtensionPart(sgAviFile) = "") : sgAviFile = sgAviFile + ".avi" : EndIf
ResultL = AVIFileOpen(@*pavfile, sgAviFile, #OF_CREATE, #Null)
If ResultL = #AVIERR_OK
strhdr\fccType = #streamtypeVIDEO
strhdr\fccHandler\a = $6D
strhdr\fccHandler\b = $73
strhdr\fccHandler\c = $76
strhdr\fccHandler\d = $63
strhdr\dwScale = 1
strhdr\dwRate = FrameRate ;##Frames per second(1 to 30)
strhdr\dwSuggestedBufferSize = bmi\bmiHeader\bisizeimage * 8
strhdr\rcFrame\top = 0
strhdr\rcFrame\left = 0
strhdr\rcFrame\right = bmi\bmiHeader\biWidth
strhdr\rcFrame\bottom = bmi\bmiHeader\biHeight
strhdr\dwStart = 0 ; StartFrame
strhdr\dwLength = ListSize(ImageList())
;strhdr\szName = @"Done by PureBasic"
PokeS(@strhdr\szName[0], "Done by PureBasic", -1, #PB_Ascii)
If AVIFileCreateStream(*pavfile, @pStream, @strhdr) = 0
Dim Opts(1)
Opts(0) = @Opts
If AVISaveOptions(WindHwnd, #ICMF_CHOOSE_KEYFRAME | #ICMF_CHOOSE_DATARATE, 1, @pStream, @opts(0)) = 1
If AVIMakeCompressedStream(@pAVICompressed, pStream, opts(0), 0) = #AVIERR_OK
If AVIStreamSetFormat(pAVICompressed, 0, @bmi, SizeOf(bitmap24)) = 0
FirstElement(ImageList())
ForEach ImageList()
If ImageList()\Buffer
If AVIStreamWrite(pAVICompressed, igIndex, 1, ImageList()\Buffer, bmi\bmiHeader\bisizeimage, #AVIIF_KEYFRAME, 0, 0) = 0
igIndex = igIndex + 1
;Debug "Wrote frame "+Str(igIndex)
Else
MessageRequester("Attention","AVI Stream Write call failed", #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
EndIf
Next
Else
MessageRequester("Attention","AVI Stream Set Format call failed", #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
AVIStreamRelease(pAVICompressed)
Else
MessageRequester("Attention","AVI Make Compressed Stream call failed", #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
Else
MessageRequester("Attention","AVI Save Options call failed", #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
AVIStreamRelease(pStream)
Else
MessageRequester("Attention","AVI create File Stream failed", #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
AVIFileRelease(*pavfile)
Else
MessageRequester("Attention","AVI File Open failed: " + Hex(ResultL, #PB_Long), #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
AVIFileExit() ;## Release the AVI Lib
;MessageRequester("Info","Total number of Frames created: " + Str(igIndex))
CloseLibrary(AviLib)
Else
MessageRequester("Attention","Could not Open AVI Lib", #PB_MessageRequester_Ok | #MB_ICONERROR)
EndIf
EndIf
EndProcedure
UseJPEGImageDecoder()
UsePNGImageDecoder()
FrameRate = 25
GetImages(500, 500, 110, 300, 80)
SaveAVI()