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

  • SEARCH
  • Home
  • 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 8107355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:44:38+00:00 2026-06-06T00:44:38+00:00

I’m trying to make a program that, using pointers, detects lines in an image

  • 0

I’m trying to make a program that, using pointers, detects lines in an image and removes those lines. Currently, the detecting lines part is working really well, and for the most part the removing the lines part is working as well. However, after around 150-200 images, the program will throw random AccessViolationExceptions in places that have nothing to do with the unsafe bits of the code.

This is the bit that does the line removal:

static unsafe Bitmap RemoveLines(Bitmap input, int[] horizontalLines, int[] verticalLines)
{
    Bitmap output;

    if (input.PixelFormat == PixelFormat.Format24bppRgb)
    {
        output = (Bitmap) input.Clone();
    }
    else
    {
        output = ConvertTo24bpp((Bitmap)input.Clone());
    }

    BitmapData bitmapData = output.LockBits(new Rectangle(0, 0, output.Width, output.Height), ImageLockMode.ReadWrite, output.PixelFormat);

    int w = output.Width;
    int h = output.Height;
    int bpp = 3;

    int s = bitmapData.Stride;

    byte* p = (byte*) bitmapData.Scan0;

    for (int r = 0; r < h; r++)
    {
        for (int c = 0; c < h; c++)
        {
            if (horizontalLines.Contains(r) || verticalLines.Contains(c))
            {
                int i = (r * s) + c * bpp;

                p[i + 0] = 255;
                p[i + 1] = 255;
                p[i + 2] = 255;
            }
        }
    }

    output.UnlockBits(bitmapData);

    return output;
}

After this code, I save the resulting Bitmap as well as embedding it in another Bitmap for comparison purposes:

// ... Detect lines and such
Bitmap export = new Bitmap(bitmap.Width * 3, bitmap.Height, PixelFormat.Format24bppRgb);
Graphics fg = Graphics.FromImage(export);
fg.DrawImage(bitmap, 0, 0); // Draw the original input bitmap
fg.DrawImage(edited, bitmap.Width, 0); // Draw the input after processing (Line Detection)
try
{
    Bitmap lineRemoved = RemoveLines(bitmap, horizontalLines.ToArray(), verticalLines.ToArray()); // Remove lines based on earlier detection
    lineRemoved.Save(cellDirectory + "\\Lines\\cell_lr_" + i.ToString("D2") + j.ToString("D2") + ".gif", ImageFormat.Gif); // Save image after removal
    fg.DrawImage(lineRemoved, bitmap.Width * 2, 0); // Add image to composite for comparison; This line is what throws the error most of the time
    lineRemoved.Dispose();
    export.Save(cellDirectory + "\\Lines\\cell" + i.ToString("D2") + j.ToString("D2") + ".gif", ImageFormat.Gif);
}
catch (Exception ex)
{ }

The DrawImage call is what throws errors, and it is always an AccessViolationException followed by an InvalidOperationException. Looking at lineRemoved during the error shows that most of its members have “threw exception of type ‘InvalidOperationException'” rather than actual values, even though one line before the same Bitmap saved just fine on its own. The input bitmap remains unchanged throughout the code, and is always Cloned or drawn to a different bitmap when I need to alter it in any way.

I’ve tried commenting out the lines after saving lineRemoved, but then the same error pops up later in the code. What’s more, that try/catch doesn’t actually catch the Exception – it always says unhandled. It’s got to be something to do with the pointers, but otherwise I am completely lost as to what is causing this.

  • 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-06T00:44:39+00:00Added an answer on June 6, 2026 at 12:44 am

    Your code contains a subtle one-character bug. The line that reads

    for (int c = 0; c < h; c++) 
    

    should be

    for (int c = 0; c < w; c++) 
    

    If the image is in landscape orientation, your bug would cause the right part of the image not being processed.

    If the image is in protrait orientation, it would cause the buffer to overflow, leading to an access violation exception (if you’re lucky) or memory corruption (if you’re not).

    That being said, your algorithm is not very efficient. For example, you are doing the calculation

    int i = (r * s) + c * bpp; 
    

    for every pixel you’re drawing, while obviously (r * s) doesn’t change in the inner loop, and c * bpp can be replaced by something like currentPixel += bpp.

    In fact it would probably be more efficient to loop over horizontalLines and verticalLines.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.