tags:

views:

505

answers:

0

I can't get this code to work... can anybody tell me what's wrong?

cropCanvas1.Image is a Bitmap object with PixelFormat = Format8bppIndexed

The resulting Bitmap is black

thanks!

        //cropCanvas1.Image.PixelFormat is Format8bppIndexed
        Bitmap bmp = new Bitmap(cropCanvas1.CropArea.Width, cropCanvas1.CropArea.Height, cropCanvas1.Image.PixelFormat);
        bmp.SetResolution(500, 500);

        bmp.Palette = cropCanvas1.Image.Palette;

        var newdata = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
        var croppeddata = cropCanvas1.Image.LockBits(new Rectangle(0, 0, cropCanvas1.Image.Width, cropCanvas1.Image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, cropCanvas1.Image.PixelFormat);

        float size = Bitmap.GetPixelFormatSize(cropCanvas1.Image.PixelFormat) / 8.0f;

        byte[] line = new byte[(int)(cropCanvas1.Image.Width * size)];

        for (int y = 0; y < bmp.Height; y++)
        {
            IntPtr source = new IntPtr(croppeddata.Scan0.ToInt32() + (y * croppeddata.Stride));
            Marshal.Copy(source, line, 0, line.Length); 

            IntPtr dest = new IntPtr(newdata.Scan0.ToInt32() + (y * newdata.Stride));
            Marshal.Copy(line, 0, newdata.Scan0, line.Length);
        }

        cropCanvas1.Image.UnlockBits(croppeddata);
        bmp.UnlockBits(newdata);

        pictureBox1.Image = bmp;