Results 1 to 3 of 3

Thread: Can DirectX's ImageFileFormat jpeg compression be set? If yes, how?

  1. #1

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Can DirectX's ImageFileFormat jpeg compression be set? If yes, how?

    I've been testing a way to screen capture with DirectX, but haven't been able to find any code to set the compression of jpegs for ImageFileFormat while saving. Is it even possible to set jpeg's compression with ImageFileFormat? Is there another way to save DirectX captured images to jpegs of a higher compression?

    The DirectX SDK I'm using is from 2010. The only version compatible with my machine. A full copy of this old SDK can be downloaded from the Wayback Machine's archive of it's Microsoft's download page:
    https://web.archive.org/web/20161203...n.aspx?id=6812

    The code below references DirectX, Direct3D, and Direct3DX dll's from this directory:
    C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.01.0.29

    The project's target framework is set to .Net 3.5 Client Profile, and under Configuration Manager, Active and Platform is set to x86.


    Update: Forgot to set the code to save jpegs. It does now, so running it shows it's crappy compression.
    Code:
    Option Strict On
    
    Imports Microsoft.DirectX
    Imports Microsoft.DirectX.Direct3D
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim present As New PresentParameters
            present.Windowed = True
            present.SwapEffect = SwapEffect.Flip
    
            Dim device As Device
            device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, present)
    
            Dim backbuffer As Surface = device.CreateOffscreenPlainSurface(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Format.A8R8G8B8, Pool.SystemMemory)
            device.GetFrontBufferData(0, backbuffer)
    
            SurfaceLoader.Save("C:\Users\Web\Desktop\Screenshot.jpg", ImageFileFormat.jpg, backbuffer)
    
            backbuffer.Dispose()
            device.Dispose()
    
        End Sub
    
    End Class
    Note: Saving a captured image as a bmp looks great (huge file size), but strangely when you save as a png, my app ignores the desktop's wallpaper, replacing it with a transparent background, and also the names of the desktop icons becomes transparent.
    Last edited by Peter Porter; Feb 14th, 2021 at 08:11 AM. Reason: Changed the code to save to jpeg's, so everyone can see it's awful compression

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    749

    Re: Can DirectX's ImageFileFormat jpeg compression be set? If yes, how?

    Not familiar with DirectX but you can save a bitmap as a jpeg like this:

    Code:
    Imports System.Drawing.Imaging
    
                Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
    
                ' Create an Encoder object based on the GUID for the Quality parameter category. 
                Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    
                ' Create an EncoderParameters object. 
                ' An EncoderParameters object has an array of EncoderParameter objects. 
                ' In this case, there is only one EncoderParameter object in the array. 
                Dim myEncoderParameters As New EncoderParameters(1)
    
                Dim myEncoderParameter As New EncoderParameter(myEncoder, 80L) ' Set quality to 80, data type = long
                myEncoderParameters.Param(0) = myEncoderParameter
    
                bm.Save("c:\test.jpg", jpgEncoder, myEncoderParameters) ' bm is your bitmap

  3. #3

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Can DirectX's ImageFileFormat jpeg compression be set? If yes, how?

    Quote Originally Posted by paulg4ije View Post
    Not familiar with DirectX but you can save a bitmap as a jpeg like this:

    Code:
    Imports System.Drawing.Imaging
    
                Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
    
                ' Create an Encoder object based on the GUID for the Quality parameter category. 
                Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    
                ' Create an EncoderParameters object. 
                ' An EncoderParameters object has an array of EncoderParameter objects. 
                ' In this case, there is only one EncoderParameter object in the array. 
                Dim myEncoderParameters As New EncoderParameters(1)
    
                Dim myEncoderParameter As New EncoderParameter(myEncoder, 80L) ' Set quality to 80, data type = long
                myEncoderParameters.Param(0) = myEncoderParameter
    
                bm.Save("c:\test.jpg", jpgEncoder, myEncoderParameters) ' bm is your bitmap
    Thanks, Paul. I'm going back to the GDI+ .

    I already know how to set jpeg's compression under GDI+, just never incorporated it into my old app since FFmpeg took cared of compression, but I will test it to see if it speeds up. My guess is the memory used to stream lossless bitmaps to FFmpeg is slowing it down, resulting in the video becoming out of sync with the audio. But if I could stream jpegs at a decent compression to FFmpeg instead of bitmaps, it should fix this syncronage problem. The memory used by jpegs at a compression where artifacts are barely noticable should be way less than the memory used by bitmaps.

    My app also has the option to use FFmpeg's gdigrab, which works great, but I can't change the region to capture on the fly that way. My app has this great feature for old LCD monitors, but it doesn't work with gdigrab.
    Last edited by Peter Porter; Feb 14th, 2021 at 02:33 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width