Click here to Skip to main content
15,887,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am using a custom class to write an image onto another image of bigger size.

I am using a custom class to write an image onto another image of bigger size.doc - Google Drive[^]

public class BitmapLockerTest
{
private static string path = @"locker.png";

public static void Run()
{
Bitmap image = Grayscale.ToGrayscale(Bitmap.FromFile(path) as Bitmap);
Bitmap canvas = Grayscale.CreateGrayscaleImage(1024, 256);
BitmapLocker imageLocker = new BitmapLocker(image);
BitmapLocker canvasLocker = new BitmapLocker(canvas);
imageLocker.Lock();
canvasLocker.Lock();
int startX = (Math.Abs(canvas.Width - image.Width) / 2);
int startY = (Math.Abs(canvas.Height - image.Height) / 2);
for (int y = startY; y < (startY + image.Height); y++)
{
for (int x = startX; x < (startX + image.Width); x++)
{
int xxx = x - startX;
int yyy = y - startY;
canvasLocker.SetPixel(x, y, imageLocker.GetPixel(xxx, yyy));
}
}

canvasLocker.Unlock();
imageLocker.Unlock();
new PictureBoxForm(canvas, image).ShowDialog();
}
}

Here is the full source code in DotNetFiddle.

Home | .NET Fiddle[^]

But, it is generating a distorted image:

http://i.stack.imgur.com/rm3Nw.png[^]

enter image description here

Probably, there has been some problems going on with reading and writing addresses.

But, I am unable to find the issue here.


P.S.2 :

Here is the input image[^]

What I have tried:

public class BitmapLockerTest
{
private static string path = @"locker.png";

public static void Run()
{
Bitmap image = Grayscale.ToGrayscale(Bitmap.FromFile(path) as Bitmap);
Bitmap canvas = Grayscale.CreateGrayscaleImage(1024, 256);
BitmapLocker imageLocker = new BitmapLocker(image);
BitmapLocker canvasLocker = new BitmapLocker(canvas);
imageLocker.Lock();
canvasLocker.Lock();
int startX = (Math.Abs(canvas.Width - image.Width) / 2);
int startY = (Math.Abs(canvas.Height - image.Height) / 2);
for (int y = startY; y < (startY + image.Height); y++)
{
for (int x = startX; x < (startX + image.Width); x++)
{
int xxx = x - startX;
int yyy = y - startY;
canvasLocker.SetPixel(x, y, imageLocker.GetPixel(xxx, yyy));
}
}

canvasLocker.Unlock();
imageLocker.Unlock();
new PictureBoxForm(canvas, image).ShowDialog();
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900