Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:16:03+00:00 2026-06-06T19:16:03+00:00

I am trying to implement an application which uses RIOT (Radical Image Optimization Tool)

  • 0

I am trying to implement an application which uses RIOT (Radical Image Optimization Tool) for batch image optimizaton. I can successfully import riot.dll into my app. But I cannot figure out how to pass a Windows DIB (Device Independent Bitmap) handle to RIOT function.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Riot_Test
{
    public partial class Form1 : Form
    {

        [DllImport("RIOT.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
        static extern bool RIOT_LoadFromDIB(IntPtr hDIB, IntPtr hwndParent, [MarshalAs(UnmanagedType.LPStr)] string fileName = "", [MarshalAs(UnmanagedType.LPStr)] string iniFile = "", int flags = 0, [MarshalAs(UnmanagedType.LPStr)] string buf = "");

        [DllImport("RIOT.dll")]
        static extern void RIOT_Show();

        [DllImport("RIOT.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
        static extern bool RIOT_LoadFromFile([MarshalAs(UnmanagedType.LPStr)] string filename, int flags = 0);

        [DllImport("RIOT.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool RIOT_SaveToFile(IntPtr hDIB, IntPtr hwndParent, [MarshalAs(UnmanagedType.LPStr)] string fileName, [MarshalAs(UnmanagedType.LPStr)] string origFilename = "", ulong byteSize = 0, [MarshalAs(UnmanagedType.LPStr)]  string errorText = "", int flags = 0, [MarshalAs(UnmanagedType.LPStr)] string buf = "");

        public Form1()
        {
            InitializeComponent();
            //RIOT_Show();
            IntPtr hdib = IntPtr.Zero;
            IntPtr hwnd = IntPtr.Zero;
            string errorText = "";

            Bitmap bmp = new Bitmap("dene.jpg");
            hdib = bmp.GetHbitmap();


            string fn = "optim2.jpg";
            string fno = "dene.jpg";
            bool result = RIOT_SaveToFile_U(hdib, hwnd, fn);


        }
    }
}

RIOT_Show() and RIOT_Load_From_File() are working as expected but when I try to call RIOT_SaveToFile() it gives me this error:

System.AccessViolationException was unhandled – Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

So my questions are:

  1. How can I convert a System.Bitmap object to Windows DIB handle ?
  2. If I can’t convert is it possible to use a C++ library (if there is) to do the job for me and return the DIB handle?

Edit:

I’ve changed my code to this but it gives the same error.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace from_vb
{
    public partial class Form1 : Form
    {
        [DllImport("gdi32.dll", SetLastError = true)]
        static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        static extern int GetObject(IntPtr hgdiobj, int cbBuffer, IntPtr lpvObject);

        [DllImport("gdi32.dll")]
        static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref GDI32BITMAPINFOHEADER pbmi, uint pila, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);

        [DllImport("gdi32.dll")]
        static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, uint uStartScan,
           uint cScanLines, [Out] byte[] lpvBits, ref GDI32BITMAPINFOHEADER lpbmi, uint uUsage);

        [DllImport("gdi32.dll")]
        static extern bool DeleteObject(IntPtr hObject);

        [DllImport("gdi32.dll")]
        static extern bool DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        static extern bool GdiFlush();

        [DllImport("RIOT.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool RIOT_SaveToFile(IntPtr hDIB, IntPtr hwndParent, [MarshalAs(UnmanagedType.LPStr)] string fileName, [MarshalAs(UnmanagedType.LPStr)] string origFilename = "", ulong byteSize = 0, [MarshalAs(UnmanagedType.LPStr)]  string errorText = "", int flags = 0, [MarshalAs(UnmanagedType.LPStr)] string buf = "");


        public const int BI_RGB = 0;
        public const int DIB_PAL_COLORS = 1;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct GDI32BITMAP
        {
            public int bmType;
            public int bmWidth;
            public int bmHeight;
            public int bmWidthBytes;
            public short bmPlanes;
            public short bmBitsPixel;
            public int bmBits;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct GDI32BITMAPINFOHEADER
        {
            public int biSize;
            public int biWidth;
            public int biHeight;
            public short biPlanes;
            public short biBitCount;
            public int biCompression;
            public int biSizeImage;
            public int biXPelsPerMeter;
            public int biYPelsPerMeter;
            public uint biClrUsed;
            public uint biClrImportant;

            public void Init()
            {
                biSize = (int)Marshal.SizeOf(this);
            }
        }



        public Form1()
        {
            InitializeComponent();
            IntPtr hdc = IntPtr.Zero;
            IntPtr hSrcBitmap = IntPtr.Zero;
            IntPtr pSrcBitmapInfo = IntPtr.Zero;
            IntPtr hDestDIBitmap = IntPtr.Zero;
            IntPtr hDstOldBitmap = IntPtr.Zero;
            IntPtr pDestDIBits = IntPtr.Zero;
            IntPtr hSection = IntPtr.Zero;
            IntPtr hwnd = IntPtr.Zero;
            IntPtr ccDC = IntPtr.Zero;

            Bitmap dotNetBitmap;
            int XDPI, YDPI;

            GDI32BITMAP srcBitmapInfo = new GDI32BITMAP();
            //ccDC = CreateCompatibleDC(hdc);
            IntPtr hDstMemDC = CreateCompatibleDC(hdc);
            GDI32BITMAPINFOHEADER DestDIBMIH = new GDI32BITMAPINFOHEADER();


            dotNetBitmap = new Bitmap("dene.jpg");
            hSrcBitmap = dotNetBitmap.GetHbitmap();
            pSrcBitmapInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(srcBitmapInfo));

            GetObject(hSrcBitmap, Marshal.SizeOf(srcBitmapInfo),pSrcBitmapInfo);
            srcBitmapInfo = (GDI32BITMAP)Marshal.PtrToStructure(pSrcBitmapInfo, srcBitmapInfo.GetType());




            if (pSrcBitmapInfo != IntPtr.Zero)
            {

                Marshal.FreeCoTaskMem(pSrcBitmapInfo);
            }

            DestDIBMIH.biSize = Marshal.SizeOf(DestDIBMIH);
            DestDIBMIH.biWidth = srcBitmapInfo.bmWidth;
            DestDIBMIH.biHeight = srcBitmapInfo.bmHeight;
            DestDIBMIH.biPlanes = srcBitmapInfo.bmPlanes;
            DestDIBMIH.biBitCount = 24;
            DestDIBMIH.biCompression = BI_RGB;

            hDestDIBitmap = CreateDIBSection(hDstMemDC, ref DestDIBMIH, 0, out pDestDIBits, hSection, 0);

            string fn = "optim2.jpg";
            string fno = "dene.jpg";
            bool hede = RIOT_SaveToFile(hDestDIBitmap, hwnd, fn);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:16:04+00:00Added an answer on June 6, 2026 at 7:16 pm

    Here is the “documentation” on that method:

    bool RIOT_SaveToFile(HANDLE hDIB, HWND hwndParent,const char *fileName,const char *origFilename=”", unsigned long byteSize=0,char *errorText=”",int flags=0, char *buf=”")
    
    Saves the file with the specified parameters.
    
    Parameters:
    hDIB – handle to a Windows DIB image in memory
    hwndParent – Not Used. Must be NULL.
    fileName – full path to the file to be saved (only JPEG is supported)
    origFilename – Fill this with the original file name if the DIB is not different than the image from the file
    byteSize – filesize of the compressed JPEG image in bytes
    errorText – On error errorText is filled with the error message
    flags – save flags. See bellow
    buf – not used. Leave blank.THe function returns true on success or false on error.If you specify byteSize you can specify any of the save flags, but the quality will not be used.
    If you don’t specify byteSize flags are used.
    If there are no flags or byteSize is 0 an error is thrown.Flags for RIOT_SaveToFile:0×0001 – keep EXIF
    0×0002 – keep IPTC
    0×0004 – keep XMP
    0×0008 – keep Comments
    0×1000 – keep ICC profile0x0800 – save greyscale
    0×2000 – save progressive
    0×10000 – disable chroma subsamplingAlso you can set a quality flag from 1 to 100Ex: flags=JPEG_SUBSAMPLING_444 | JPEG_PROGRESSIVE | 90
    results in a file with no subsampling, progressive with a quality of 90
    If you don’t specify quality 75 is used.
    You must specify JPEG_PROGRESSIVE if you want progressive.
    If you don’t specify JPEG_SUBSAMPLING_444 a default chroma of 4:2:0 is used.
    

    The first issue is with errorText. The doc states, “On error errorText is filled with the error message.” This suggests that you have to pass an array of bytes of un-specified length that will get filled in. That’s hideous.

    The second issue is where it says, “If there are no flags or byteSize is 0 an error is thrown.”

    My guess is that you’re getting an error and it’s trying to copy bytes into errorText. Try passing a largish byte[] instead of a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement an application which would take an image from the
I'm trying to implement a PDF output target in an existing application which uses
I am trying to implement the blackberry application which will help the user to
I'm trying to implement OSGI bundle with network server which uses network sockets. This
I am trying to implement a delegate method for my Cocoa document-based application which
I am trying to build an application which the user can use to draw
I am trying to implement a protocol which I will use for my application
I'm trying to implement Application Tests as described here . So far, so good,
we are trying to implement an application using the Service Layer Pattern cause our
I am trying to implement UITableview based application. In my tableView their is 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.