Help: SaveImage() vs. AllocateMemory()

Just starting out? Need help? Post your questions and find answers here.
scriptmaster
User
User
Posts: 15
Joined: Fri Mar 13, 2009 3:13 pm
Location: Chennai, India

Help: SaveImage() vs. AllocateMemory()

Post by scriptmaster »

Hi all,

I am new to purebasic. I am writing a screen capture program where I am capturing the screen image into a #Image. Now I need the memory contents of #Image to be sent over network with compression, but I do not want to use SaveImage(). Would it be faster to not write to the disk but directly send the memory contents using SendNetworkData()? If so, how do I dump/read the memory contents of a #Image without using SaveImage()? I am not sure how to proceed after AllocateMemory()

Is there a way to do this with existing PB functions? Thanks in advance for your help
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Help: SaveImage() vs. AllocateMemory()

Post by idle »

for windows only

Code: Select all


Procedure CopyImageToMemory(ImageNumber, Memory)

   Protected TemporaryDC.L, TemporaryBitmap.BITMAP, TemporaryBitmapInfo.BITMAPINFO

    TemporaryDC = CreateDC_("DISPLAY", #Null, #Null, #Null)
   
    GetObject_(ImageID(ImageNumber), SizeOf(BITMAP), TemporaryBitmap.BITMAP)
       
    TemporaryBitmapInfo\bmiHeader\biSize        = SizeOf(BITMAPINFOHEADER)
    TemporaryBitmapInfo\bmiHeader\biWidth       = TemporaryBitmap\bmWidth
    TemporaryBitmapInfo\bmiHeader\biHeight      = -TemporaryBitmap\bmHeight
    TemporaryBitmapInfo\bmiHeader\biPlanes      = 1
    TemporaryBitmapInfo\bmiHeader\biBitCount    = 32
    TemporaryBitmapInfo\bmiHeader\biCompression = #BI_RGB

    GetDIBits_(TemporaryDC, ImageID(ImageNumber), 0, TemporaryBitmap\bmHeight, Memory, TemporaryBitmapInfo, #DIB_RGB_COLORS)

    DeleteDC_(TemporaryDC)
   
EndProcedure

Procedure CopyMemoryToImage(Memory, ImageNumber)
 
     Protected TemporaryDC.L, TemporaryBitmap.BITMAP, TemporaryBitmapInfo.BITMAPINFO
 
    TemporaryDC = CreateDC_("DISPLAY", #Null, #Null, #Null)
   
    GetObject_(ImageID(ImageNumber), SizeOf(BITMAP), TemporaryBitmap.BITMAP)

    TemporaryBitmapInfo\bmiHeader\biSize        = SizeOf(BITMAPINFOHEADER)
    TemporaryBitmapInfo\bmiHeader\biWidth       = TemporaryBitmap\bmWidth
    TemporaryBitmapInfo\bmiHeader\biHeight      = -TemporaryBitmap\bmHeight
    TemporaryBitmapInfo\bmiHeader\biPlanes      = 1
    TemporaryBitmapInfo\bmiHeader\biBitCount    = 32
    TemporaryBitmapInfo\bmiHeader\biCompression = #BI_RGB
   
    SetDIBits_(TemporaryDC, ImageID(ImageNumber), 0, TemporaryBitmap\bmHeight, Memory, TemporaryBitmapInfo, #DIB_RGB_COLORS)
   
    DeleteDC_(TemporaryDC)
 
EndProcedure

Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Help: SaveImage() vs. AllocateMemory()

Post by Fred »

You can use DrawingBuffer() as well to get direct pixel access.
Thorium
Addict
Addict
Posts: 1308
Joined: Sat Aug 15, 2009 6:59 pm

Re: Help: SaveImage() vs. AllocateMemory()

Post by Thorium »

You can use the include for my image format. It can encode a PB image without writing it to disk.
http://www.purebasic.fr/english/viewtop ... 27&t=39456
Download it, include it into your project.

Then you can use "Tsi_Save2MemoryFromImage" to encode your image. If you want to send it over the network don't use "Tsi_BestSave2MemoryFromImage" because it's much slower, instead use "Tsi_Save2MemoryFromImage" where you have to specify compression, filter and transformation settings, so the encoder don't have to test for the best combination.

Use it like that:

Code: Select all

Define.i EncodedImageSize
Define *EncodedImage

*EncodedImage = Tsi_Save2MemoryFromImage(PBImage, @ImageSize, #Tsi_Filter_Up, #Tsi_Transformation_AlphaSort, #Tsi_Compression_Zlib, 9)
That will likely result in a good compression ratio.
*EncodedImage is the pointer to the encoded image and EncodedImageSize is the size of the image in bytes. So you just can send it over network and on the other side use "Tsi_CatchTsi2Image(*Tsi)" it's basicly the same as PB's CatchImage but will catch a TSI and returns a PB image number.
Post Reply