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 8912103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:07:00+00:00 2026-06-15T04:07:00+00:00

I’m trying to do image processing on the GPU with .NET. I’ve downloaded OpenCL.NET

  • 0

I’m trying to do image processing on the GPU with .NET. I’ve downloaded OpenCL.NET wrapper. It has some good samples, but I cannot find a way to load an image to the GPU and read the processed image back. What do I have to do?

  • 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-15T04:07:02+00:00Added an answer on June 15, 2026 at 4:07 am

    After setting up the context, do the following:

    public void ImagingTest (string inputImagePath, string outputImagePath)
    {
        Cl.ErrorCode error;
    
        //Load and compile kernel source code.
        string programPath = Environment.CurrentDirectory + "/../../ImagingTest.cl";    //The path to the source file may vary
    
        if (!System.IO.File.Exists (programPath)) {
            Console.WriteLine ("Program doesn't exist at path " + programPath);
            return;
        }
    
        string programSource = System.IO.File.ReadAllText (programPath);
    
        using (Cl.Program program = Cl.CreateProgramWithSource(_context, 1, new[] { programSource }, null, out error)) {
            CheckErr(error, "Cl.CreateProgramWithSource");
    
            //Compile kernel source
            error = Cl.BuildProgram (program, 1, new[] { _device }, string.Empty, null, IntPtr.Zero);
            CheckErr(error, "Cl.BuildProgram");
    
            //Check for any compilation errors
            if (Cl.GetProgramBuildInfo (program, _device, Cl.ProgramBuildInfo.Status, out error).CastTo<Cl.BuildStatus>()
                != Cl.BuildStatus.Success) {
                CheckErr(error, "Cl.GetProgramBuildInfo");
                Console.WriteLine("Cl.GetProgramBuildInfo != Success");
                Console.WriteLine(Cl.GetProgramBuildInfo(program, _device, Cl.ProgramBuildInfo.Log, out error));
                return;
            }
    
            //Create the required kernel (entry function)
            Cl.Kernel kernel = Cl.CreateKernel(program, "imagingTest", out error);
            CheckErr(error, "Cl.CreateKernel");
    
            int intPtrSize = 0;
            intPtrSize = Marshal.SizeOf(typeof(IntPtr));
    
            //Image's RGBA data converted to an unmanaged[] array
            byte[] inputByteArray;
            //OpenCL memory buffer that will keep our image's byte[] data.
            Cl.Mem inputImage2DBuffer;
    
            Cl.ImageFormat clImageFormat = new Cl.ImageFormat(Cl.ChannelOrder.RGBA, Cl.ChannelType.Unsigned_Int8);
    
            int inputImgWidth, inputImgHeight;
    
            int inputImgBytesSize;
    
            int inputImgStride;
    
            //Try loading the input image
            using (FileStream imageFileStream = new FileStream(inputImagePath, FileMode.Open) ) {
                System.Drawing.Image inputImage = System.Drawing.Image.FromStream( imageFileStream );
    
                if (inputImage == null) {
                    Console.WriteLine("Unable to load input image");
                    return;
                }
    
                inputImgWidth = inputImage.Width;
                inputImgHeight = inputImage.Height;
    
                System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(inputImage);
    
                //Get raw pixel data of the bitmap
                //The format should match the format of clImageFormat
                BitmapData bitmapData = bmpImage.LockBits( new Rectangle(0, 0, bmpImage.Width, bmpImage.Height),
                                                          ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);//inputImage.PixelFormat);
    
                inputImgStride = bitmapData.Stride;
                inputImgBytesSize = bitmapData.Stride * bitmapData.Height;
    
                //Copy the raw bitmap data to an unmanaged byte[] array
                inputByteArray = new byte[inputImgBytesSize];
                Marshal.Copy(bitmapData.Scan0, inputByteArray, 0, inputImgBytesSize);
    
                //Allocate OpenCL image memory buffer
                inputImage2DBuffer = Cl.CreateImage2D(_context, Cl.MemFlags.CopyHostPtr | Cl.MemFlags.ReadOnly, clImageFormat,
                                                    (IntPtr)bitmapData.Width, (IntPtr)bitmapData.Height,
                                                    (IntPtr)0, inputByteArray, out error);
                CheckErr(error, "Cl.CreateImage2D input");
            }
    
            //Unmanaged output image's raw RGBA byte[] array
            byte[] outputByteArray = new byte[inputImgBytesSize];
    
            //Allocate OpenCL image memory buffer
            Cl.Mem outputImage2DBuffer = Cl.CreateImage2D(_context, Cl.MemFlags.CopyHostPtr | Cl.MemFlags.WriteOnly, clImageFormat,
                                                          (IntPtr)inputImgWidth, (IntPtr)inputImgHeight, (IntPtr)0, outputByteArray, out error);
            CheckErr(error, "Cl.CreateImage2D output");
    
            //Pass the memory buffers to our kernel function
            error = Cl.SetKernelArg(kernel, 0, (IntPtr)intPtrSize, inputImage2DBuffer);
            error |= Cl.SetKernelArg(kernel, 1, (IntPtr)intPtrSize, outputImage2DBuffer);
            CheckErr(error, "Cl.SetKernelArg");
    
            //Create a command queue, where all of the commands for execution will be added
            Cl.CommandQueue cmdQueue = Cl.CreateCommandQueue(_context, _device, (Cl.CommandQueueProperties)0, out error);
            CheckErr(error, "Cl.CreateCommandQueue");
    
            Cl.Event clevent;
    
            //Copy input image from the host to the GPU.
            IntPtr[] originPtr = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };  //x, y, z
            IntPtr[] regionPtr = new IntPtr[] { (IntPtr)inputImgWidth, (IntPtr)inputImgHeight, (IntPtr)1 }; //x, y, z
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)inputImgWidth, (IntPtr)inputImgHeight, (IntPtr)1 };
            error = Cl.EnqueueWriteImage(cmdQueue, inputImage2DBuffer, Cl.Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, inputByteArray, 0, null, out clevent);
            CheckErr(error, "Cl.EnqueueWriteImage");
    
            //Execute our kernel (OpenCL code)
            error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
            CheckErr(error, "Cl.EnqueueNDRangeKernel");
    
            //Wait for completion of all calculations on the GPU.
            error = Cl.Finish(cmdQueue);
            CheckErr(error, "Cl.Finish");
    
            //Read the processed image from GPU to raw RGBA data byte[] array
            error = Cl.EnqueueReadImage(cmdQueue, outputImage2DBuffer, Cl.Bool.True, originPtr, regionPtr,
                                        (IntPtr)0, (IntPtr)0, outputByteArray, 0, null, out clevent);
            CheckErr(error, "Cl.clEnqueueReadImage");
    
            //Clean up memory
            Cl.ReleaseKernel(kernel);
            Cl.ReleaseCommandQueue(cmdQueue);
    
            Cl.ReleaseMemObject(inputImage2DBuffer);
            Cl.ReleaseMemObject(outputImage2DBuffer);
    
            //Get a pointer to our unmanaged output byte[] array
            GCHandle pinnedOutputArray = GCHandle.Alloc(outputByteArray, GCHandleType.Pinned);
            IntPtr outputBmpPointer = pinnedOutputArray.AddrOfPinnedObject();
    
            //Create a new bitmap with processed data and save it to a file.
            Bitmap outputBitmap = new Bitmap(inputImgWidth, inputImgHeight, inputImgStride, PixelFormat.Format32bppArgb, outputBmpPointer);
    
            outputBitmap.Save(outputImagePath, System.Drawing.Imaging.ImageFormat.Png);
    
            pinnedOutputArray.Free();
        }
    }
    

    OpenCL kernel used in this example:

    __kernel void imagingTest(__read_only  image2d_t srcImg, 
                           __write_only image2d_t dstImg)
    {
      const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | //Natural coordinates
        CLK_ADDRESS_CLAMP_TO_EDGE | //Clamp to zeros
        CLK_FILTER_LINEAR;
    
      int2 coord = (int2)(get_global_id(0), get_global_id(1)); 
    
      uint4 bgra = read_imageui(srcImg, smp, coord);    //The byte order is BGRA 
    
      float4 bgrafloat = convert_float4(bgra) / 255.0f; //Convert to normalized [0..1] float
    
      //Convert RGB to luminance (make the image grayscale).
      float luminance =  sqrt(0.241f * bgrafloat.z * bgrafloat.z + 0.691f * bgrafloat.y * bgrafloat.y + 0.068f * bgrafloat.x * bgrafloat.x);
      bgra.x = bgra.y = bgra.z = (uint) (luminance * 255.0f);
    
      bgra.w = 255;
    
      write_imageui(dstImg, coord, bgra);
    }
    

    *Complete article available at codeproject.com

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.