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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:12+00:00 2026-05-22T01:17:12+00:00

public unsafe Bitmap MedianFilter(Bitmap Img) { int Size =2; List<byte> R = new List<byte>();

  • 0
public unsafe Bitmap  MedianFilter(Bitmap Img)
     {
        int Size =2;

        List<byte> R = new List<byte>();
        List<byte> G = new List<byte>();
        List<byte> B = new List<byte>();

        int ApetureMin = -(Size / 2);
        int ApetureMax = (Size / 2);

        BitmapData imageData = Img.LockBits(new Rectangle(0, 0, Img.Width, Img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);

        byte* start = (byte*)imageData.Scan0.ToPointer ();
        for (int x = 0; x < imageData.Width; x++)
        {
            for (int y = 0; y < imageData.Height; y++)
            {
                for (int x1 = ApetureMin; x1 < ApetureMax; x1++)
                {
                    int valx = x + x1;
                    if (valx >= 0 && valx < imageData.Width)
                    {
                        for (int y1 = ApetureMin; y1 < ApetureMax; y1++)
                        {
                            int valy = y + y1;
                            if (valy >= 0 && valy < imageData.Height)
                            {
                                Color tempColor = Img.GetPixel(valx, valy);// error come from here
                                R.Add(tempColor.R);
                                G.Add(tempColor.G);
                                B.Add(tempColor.B);
                            }
                        }
                    }
                }
            }
        }

        R.Sort();
        G.Sort();
        B.Sort();

        Img.UnlockBits(imageData);
        return Img;
     }

I tried to do this. but i got an error call “Bitmap region is already locked” can anyone help how to solve this. (error position is highlighted)

  • 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-05-22T01:17:12+00:00Added an answer on May 22, 2026 at 1:17 am

    GetPixel is the slooow way to access the image and doesn’t work (as you noticed) anymore if someone else starts messing with the image buffer directly. Why would you want to do that?

    Check Using the LockBits method to access image data for some good insight into fast image manipulation.

    In this case, use something like this instead:

    int pixelSize = 4 /* Check below or the site I linked to and make sure this is correct */
    byte* color =(byte *)imageData .Scan0+(y*imageData .Stride) + x * pixelSize;
    

    Note that this gives you the first byte for that pixel. Depending on the color format you are looking at (ARGB? RGB? ..) you need to access the following bytes as well. Seems to suite your usecase anyway, since you just care about byte values, not the Color value.


    So, after having some spare minutes, this is what I’d came up with (please take your time to understand and check it, I just made sure it compiles):

        public void SomeStuff(Bitmap image)
        {
            var imageWidth = image.Width;
            var imageHeight = image.Height;
            var imageData = image.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
    
            var imageByteCount = imageData.Stride*imageData.Height;
            var imageBuffer = new byte[imageByteCount];
            Marshal.Copy(imageData.Scan0, imageBuffer, 0, imageByteCount);
    
            for (int x = 0; x < imageWidth; x++)
            {
                for (int y = 0; y < imageHeight; y++)
                {
                    var pixelColor = GetPixel(imageBuffer, imageData.Stride, x, y);
    
                    // Do your stuff
                }
            }
        }
    
        private static Color GetPixel(byte[] imageBuffer, int imageStride, int x, int y)
        {
    
            int pixelBase = y*imageStride + x*3;
            byte blue = imageBuffer[pixelBase];
            byte green = imageBuffer[pixelBase + 1];
            byte red = imageBuffer[pixelBase + 2];
    
            return Color.FromArgb(red, green, blue);
        }
    

    This

    • Relies on the PixelFormat you used in your sample (regarding both the pixelsize/bytes per pixel and the order of the values). If you change the PixelFormat this will break.
    • Doesn’t need the unsafe keyword. I doubt that it makes a lot of difference, but you are free to use the pointer based access instead, the method would be the same.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my bitmap creation code public static Bitmap Plot24(ref byte[] bufferArray, int lengthOfBufferArray,
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
I have follow methods in Java: public abstract int AMRecoveryModeDeviceSetAutoBoot(am_recovery_device paramam_recovery_device, byte paramByte); public
I have such class public unsafe class EigenSolver { public double* aPtr {get; private
public class MyClass { public int Age; public int ID; } public void MyMethod()
public static IList<T> LoadObjectListAll<T>() { ISession session = CheckForExistingSession(); var cfg = new NHibernate.Cfg.Configuration().Configure();
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElement methodCaller
public class Item { ... } public class Order { public List<Item> Items ...
I use C#.net. These is my method now: [DllImport(DLLPath, CallingConvention = CallingConvention.Cdecl)] unsafe public
I use an extension method to convert float arrays into byte arrays: public static

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.