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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:02:18+00:00 2026-06-01T23:02:18+00:00

I am cutting and pasting from one 1bpp indexed image to a new image.

  • 0

I am cutting and pasting from one 1bpp indexed image to a new image.

All works well until the starting pixel is a divisor of 8. In the code below stride is equal to a value relative to the width of the rectangle until I hit a byte boundary. Then the stride is equal to the width of the entire page.

var croppedRect = new Rectangle((int)left, (int)top, (int)width, (int)height);
BitmapData croppedSource = _bitmapImage.LockBits(croppedRect, ImageLockMode.ReadWrite, BitmapImage.PixelFormat);
int stride = croppedSource.Stride;

This is a problem because rather than pasting my selected area into the new image, the Marshal copies a cross section, the height of the selected area, of the entire width of the page.

int numBytes = stride * (int)height;
var srcData = new byte[numBytes];

Marshal.Copy(croppedSource.Scan0, srcData, 0, numBytes);
Marshal.Copy(srcData, 0, croppedDest.Scan0, numBytes);
destBmp.UnlockBits(croppedDest);
  • 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-01T23:02:20+00:00Added an answer on June 1, 2026 at 11:02 pm

    Here’s my code for anyone who’s interested. There may be a more optimal solution but this works. I am creating an entire page in white and duplicating the selected area in the new page as I pass over it. Thanks to Bob Powell for the SetIndexedPixel routine.

    protected int GetIndexedPixel(int x, int y, BitmapData bmd)
    {
        var index = y * bmd.Stride + (x >> 3);
        var p = Marshal.ReadByte(bmd.Scan0, index);
        var mask = (byte)(0x80 >> (x & 0x7));
        return p &= mask;
    }
    
    protected void SetIndexedPixel(int x, int y, BitmapData bmd, bool pixel)
    {
        int index = y * bmd.Stride + (x >> 3);
        byte p = Marshal.ReadByte(bmd.Scan0, index);
        byte mask = (byte)(0x80 >> (x & 0x7));
        if (pixel)
            p &= (byte)(mask ^ 0xff);
        else
            p |= mask;
        Marshal.WriteByte(bmd.Scan0, index, p);
    }
    
    public DocAppImage CutToNew(int left, int top, int width, int height, int pageWidth, int pageHeight)
    {
        var destBmp = new Bitmap(pageWidth, pageHeight, BitmapImage.PixelFormat);
        var pageRect = new Rectangle(0, 0, pageWidth, pageHeight);
    
        var pageData = destBmp.LockBits(pageRect, ImageLockMode.WriteOnly, BitmapImage.PixelFormat);
        var croppedRect = new Rectangle(left, top, width, height);
        var croppedSource = BitmapImage.LockBits(croppedRect, ImageLockMode.ReadWrite, BitmapImage.PixelFormat);
    
        for (var y = 0; y < pageHeight; y++)
            for (var x = 0; x < pageWidth; x++)
            {
                if (y >= top && y <= top + height && x >= left && x <= width + left)
                {
                    SetIndexedPixel(x, y, pageData,
                                    GetIndexedPixel(x - left, y - top, croppedSource) == 0 ? true : false);
                    SetIndexedPixel(x - left, y - top, croppedSource, false); //Blank area in original
                }
                else
                    SetIndexedPixel(x, y, pageData, false);  //Fill the remainder of the page with white.
            }
    
        destBmp.UnlockBits(pageData);
    
        var retVal = new DocAppImage { BitmapImage = destBmp };
        destBmp.Dispose();
    
        BitmapImage.UnlockBits(croppedSource);
        SaveBitmapToFileImage(BitmapImage);
    
        return retVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to keep from cutting and pasting code so I made a wrapper
is it possible to transfer files using vb.net? Cutting the files from a certain
Guys I have been trying lots of different options from cutting up to building
Well my macbook pro is kind of not cutting it for me anymore and
Short of cutting and pasting, is there a way to sort the methods in
I find that I am cutting and pasting a LOT when I program. For
Can anyone give an idea of how should I implement undo/redo of cutting/copying/pasting of
Before now I've just been cutting and pasting code into my .emacs file, but
I am cutting and joining a video file using mencoder. But the output file
I found this post on Crystal Reports Cutting Off Text in PDF , but

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.