So i got an idea in class a while ago and thought it would be a good learning experience. Completely useless, but fun to stare at.
what it does:
takes a picture (jpg format), then makes it look wavy using a sin wave.
It started out pretty easy until i realized that using GetPixel and SetPixel is slow as balls. The alternative was editing memory directly
btw, if anyone knows how to edit a picture using pointers vertically instead of horizonatally, please let me know. My brain shutdown when i tried to figure this out, so im just rotating the image and changing it again. I know the theory of how to do it but the code eludes me.
heres the fun code:
Code: c#
- private void makeMovingImage()
- {
- Image temp;
- imageArray = new Bitmap[num];
- for (int x = 0; x < num; x++)
- {
- temp = image2;
- imageArray[x] = makeNewImage(temp);
- temp = new Bitmap(imageArray[x]);
- temp.RotateFlip(RotateFlipType.Rotate90FlipNone);
- temp = makeNewImage(temp);
- temp.RotateFlip(RotateFlipType.Rotate270FlipNone);
- imageArray[x] = new Bitmap(temp);
- ctr += .2;
- }
- }
- private unsafe Bitmap makeNewImage(Image newimg)
- {
- Bitmap image = new Bitmap(newimg);
- Bitmap newImage = new Bitmap(image.Width + 20, image.Height);
- Rectangle imageRect = new Rectangle(0, 0, image.Width, image.Height);
- Rectangle newImageRect = new Rectangle(0, 0, newImage.Width, newImage.Height);
- BitmapData imageData = image.LockBits(imageRect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- BitmapData newImageData = newImage.LockBits(newImageRect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- int oldSize = imageData.Stride * image.Height;
- int newSize = newImageData.Stride * newImage.Height;
- byte[] oldData = new byte[oldSize];
- byte[] newData = new byte[newSize];
- System.Runtime.InteropServices.Marshal.Copy(imageData.Scan0, oldData, 0, oldSize);
- System.Runtime.InteropServices.Marshal.Copy(newImageData.Scan0, newData, 0, newSize);
- byte* oldPointer = (byte*)imageData.Scan0;
- byte* newPointer = (byte*)newImageData.Scan0;
- int offset = 0;
- int pixel = 0;
- for (int y = 0; y < image.Height; y++)
- {
- offset = (int)Math.Round((10 * Math.Sin(ctr)));
- ctr += .2;
- offset += 10;
- for (int counter = 0; counter < offset; counter++)
- {
- newPointer[0] = 0;
- newPointer[1] = 255;
- newPointer[2] = 0;
- newPointer += 3;
- pixel++;
- }
- for (int x = 0; x < image.Width; x++)
- {
- newPointer[0] = oldPointer[0];
- newPointer[1] = oldPointer[1];
- newPointer[2] = oldPointer[2];
- oldPointer += 3;
- newPointer += 3;
- pixel++;
- }
- for (int counter = pixel; counter < newImage.Width; counter++)
- {
- newPointer[0] = 0;
- newPointer[1] = 255;
- newPointer[2] = 0;
- newPointer += 3;
- }
- pixel = 0;
- oldPointer += imageData.Stride - (imageData.Width * 3);
- newPointer += newImageData.Stride - (newImageData.Width * 3);
- }
- image.UnlockBits(imageData);
- newImage.UnlockBits(newImageData);
- return newImage;
- }
I use this code with a kinda double buffered form to show a moving picture. if anyone wants the full source code, i have no problems sending it, i just dont want to show off my horrible coding practices
will post pics after i finish my visual studio 2008 install