Skip to content

Instantly share code, notes, and snippets.

@wavescholar
Created April 25, 2014 17:14
Show Gist options
  • Select an option

  • Save wavescholar/11296715 to your computer and use it in GitHub Desktop.

Select an option

Save wavescholar/11296715 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using klManagedImaging;
using System.Drawing;
//Quite Literally - scraps
//Scraps of Bitmap Interop GDI+ code I did not want to loose.
namespace ImageInterop
{
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern int BitBlt(IntPtr hdcDst, int xDst, int yDst, int w, int h, IntPtr hdcSrc, int xSrc, int ySrc, int rop);
static int SRCCOPY = 0x00CC0020;
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int InvalidateRect(IntPtr hwnd, IntPtr rect, int bErase);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct BITMAPINFO
{
public uint biSize;
public int biWidth, biHeight;
public short biPlanes, biBitCount;
public uint biCompression, biSizeImage;
public int biXPelsPerMeter, biYPelsPerMeter;
public uint biClrUsed, biClrImportant;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 256)]
public uint[] cols;
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO bmi, uint Usage, out IntPtr bits, IntPtr hSection, uint dwOffset);
static uint BI_RGB = 0;
static uint DIB_RGB_COLORS = 0;
static uint MAKERGB(int r, int g, int b)
{
return ((uint)(b & 255)) | ((uint)((r & 255) << 8)) | ((uint)((g & 255) << 16));
}
private void SomeBitmapCode()
{
int width = 512;
int height = 1024;
int bands = 1;
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
int stride = bmpData.Stride;
byte[] rgbValues = new byte[stride* bmp.Height];
Double[,] greyVal = mat.ToArray( );
int rgbIndexer=0;
int strideJumpBytes = (stride - width);
for (int x = 0; x < greyVal.GetLength(0); x += 1)
{
Vector row = mat.RowVector(x);
for (int y = 0; y < greyVal.GetLength(1); y += 1)
{
rgbValues[rgbIndexer]= (byte)row[y];
rgbIndexer += 1;
}
rgbIndexer += strideJumpBytes;
}
IntPtr ptr = bmpData.Scan0;
System.Drawing.Imaging.ColorPalette myPal=bmp.Palette;
int ncols = 256;
Color[] colArray = new Color[ncols];
for (int i = 0; i < ncols; i++)
{
uint col= MAKERGB(i, i, i);
System.Drawing.Color myCol =Color.FromArgb( 0, (int)col, (int)col,(int) col );
myPal.Entries[i] = myCol;
}
bmp.Palette = myPal;
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, stride * bmp.Height);
bmp.UnlockBits(bmpData);
pictureBox1.Image = bmp;
bmp.Save("c:/temp/gaussianBitmap.jpg");
}
class TestManagedImageing
{
static void test(string[] args)
{
Bitmap img = new Bitmap("C:\\temp\\img.jpg");
int x0=0;
int y0=0;
int w=img.Width;
int h=img.Height;
String id = "img";
System.Drawing.Imaging.PixelFormat format= img.PixelFormat;
Rectangle rect= new Rectangle(0, 0,w, h);
BitmapData bmd=img.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite, format);
IntPtr data = bmd.Scan0;
int bytes = img.Width * img.Height * 3;
byte[] _imageBuffer = new byte[bytes];
IntPtr bmpdata = new IntPtr();
bmpdata =bmd.Scan0;
System.Runtime.InteropServices.Marshal.Copy( bmpdata, _imageBuffer, 0, bytes );
img.Save("c:/temp/ippManaged_ProcessedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
img.UnlockBits( bmd);
klImageTile tile = new klImageTile(x0, y0, w, h, id, _imageBuffer);
klImageOp kliop=new klImageOp();
kliop.Init("c:\\temp\\out.rts",w,h);
kliop.OperateTile(tile, "op");
kliop.OperateTile(tile, "op");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment