/// <summary> /// バッファーから生成 /// </summary> /// <param name="w">ウイジェット</param> /// <param name="buffer">バッファー</param> /// <param name="width">幅</param> /// <param name="height">高さ</param> /// <param name="depth">色深度</param> /// <param name="bpp">BPP</param> /// <returns>XImageのインスタンス</returns> public static XImage FromBuffer(Widgets.IWidget w, byte[] buffer,int width, int height,int depth, int bpp) { var im = new XImage(); im.convertBuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(byte)) * (buffer.Length+1)); Marshal.Copy(buffer, 0, im.convertBuffer, buffer.Length); im.image = NativeMethods.XCreateImage(w.NativeHandle.Display, 0, (uint)depth, Format.ZPixmap, 0, im.convertBuffer, (uint)width, (uint)height, bpp, 0); im.Width = width; im.Height = height; return im; }
internal static extern IntPtr XCreateImage(IntPtr display, int visual, uint depth, XImage.Format format, int offset, IntPtr data, uint width, uint height, int bitmap_pad, int bytes_per_line);
/// <summary> /// System.Drawing.Bitmapから生成 /// </summary> /// <param name="w">ウイジェット</param> /// <param name="bitmap">bitmap</param> /// <returns>XImageのインスタンス</returns> public static XImage FromBitmap(Widgets.IWidget w, System.Drawing.Bitmap bitmap) { var im = new XImage(); var data = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int bytes = bitmap.Width * bitmap.Height * 4; byte[] buf = new byte[bytes]; Marshal.Copy(data.Scan0, buf, 0, buf.Length); bitmap.UnlockBits(data); im.convertBuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(byte)) * (buf.Length+1)); Marshal.Copy(buf, 0, im.convertBuffer, buf.Length); im.image = NativeMethods.XCreateImage(w.NativeHandle.Display, 0, 24, Format.ZPixmap, 0, im.convertBuffer, (uint)bitmap.Width, (uint)bitmap.Height, 32, 0); im.Width = bitmap.Width; im.Height = bitmap.Height; return im; }