How to load bmp file using x11 window background
XReadBitmapFile is for reading .xbm files only. What is needed is a library for reading BMP files, one possibility is ImLib2 which can read numerous types of files and works well with Xlib.
Here is a longish example of using it:
/* displays an image or sets root background
* PUBLIC DOMAIN - CC0 http://creativecommons.org/publicdomain/zero/1.0/
* J.Mayo 2013
*
* gcc -Wall -W -g3 -o xrootbg xrootbg.c -lX11 -lImlib2
*
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <Imlib2.h>
int main(int argc, char **argv)
{
Imlib_Image img;
Display *dpy;
Pixmap pix;
Window root;
Screen *scn;
int width, height;
const char *filename = NULL;
if (argc < 2)
goto usage;
filename = argv[1];
img = imlib_load_image(filename);
if (!img) {
fprintf(stderr, "%s:Unable to load image\n", filename);
goto usage;
}
imlib_context_set_image(img);
width = imlib_image_get_width();
height = imlib_image_get_height();
dpy = XOpenDisplay(NULL);
if (!dpy)
return 0;
scn = DefaultScreenOfDisplay(dpy);
root = DefaultRootWindow(dpy);
pix = XCreatePixmap(dpy, root, width, height,
DefaultDepthOfScreen(scn));
imlib_context_set_display(dpy);
imlib_context_set_visual(DefaultVisualOfScreen(scn));
imlib_context_set_colormap(DefaultColormapOfScreen(scn));
imlib_context_set_drawable(pix);
imlib_render_image_on_drawable(0, 0);
XSetWindowBackgroundPixmap(dpy, root, pix);
XClearWindow(dpy, root);
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
}
XFreePixmap(dpy, pix);
imlib_free_image();
XCloseDisplay(dpy);
return 0;
usage:
fprintf(stderr, "usage: %s <image_file>\n", argv[0]);
return 1;
}
How to display bitmap image in background Win32
You are not performing any error checking in the call to LoadBitmap
and the call to CreatePatternBrush
. Very likely one of these calls fails and so hbrBackground
is set to NULL
, which results in a white background.
Your next step is to do some debugging to work out which call fails. Quite likely is that the bitmap file that you are linking as a resource uses a format that is not supported by LoadBitmap
. Or perhaps the bitmap file is somehow not being linked. But so long as you actually manage to create a bitmap and then a brush, the system will use that brush to paint the background.
C++ (LINUX) set X11 window background with DevIL
The Xlib function used to create an XImage
is XCreateImage, and its usage looks like this (you can read a full description in the link):
XImage *XCreateImage(display, visual, depth, format, offset, data,
width, height, bitmap_pad, bytes_per_line)
where the relevant argument for your specific question would be data, a char*
that points to where you keep the image data loaded in with DevIL. With this, you should then be able to follow the steps in the other answer you already found.
Edited to add:
You still have to tell DevIL how to format your image data so that XCreateImage can understand it. For example the following pair of function calls will create an XImage that appears correctly:
ilCopyPixels(
0, 0, 0,
image_width, image_height, 1,
IL_BGRA, IL_UNSIGNED_BYTE,
image_data
);
// ...
XImage* background = XCreateImage(
display,
XDefaultVisual(display, XDefaultScreen(display)),
XDefaultDepth(display, XDefaultScreen(display)),
ZPixmap,
0,
image_data,
image_width,
image_height,
32,
0
);
, if you instead chose IL_RGBA
, the colors will be off!
Putting image into a Window in x11
Alright, solved it on my own. There was a silly mistake at changing the pixel value and updating it to the actual image and then putting it to the background of the window.
First use XPutPixel()
, then use XPutImage()
Here is the final and correct method:
// compilation:
// g++ -o go qrinX11.cpp `pkg-config --cflags --libs opencv` -lX11
//
#include <opencv2/opencv.hpp>
#include "opencv2/opencv.hpp" // FOR OpenCV
#include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <bits/stdc++.h>
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <unistd.h> // So we got the profile for 10 seconds
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xlib.h> // Every Xlib program must include this
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/shape.h>
#define NIL (0) // A name for the void pointer
using namespace cv;
using namespace std;
int main()
{
XGCValues gr_values;
//GC gc;
XColor color, dummy;
Display *dpy = XOpenDisplay(NIL);
//assert(dpy);
//int screen = DefaultScreen(dpy);
// Get some colors
int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
// Create the window
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, whiteColor, blackColor);
// We want to get MapNotify events
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
// Wait for the MapNotify event
for(;;) {
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}
Window focal = w;
XWindowAttributes gwa;
XGetWindowAttributes(dpy, w, &gwa);
int wd1 = gwa.width;
int ht1 = gwa.height;
XImage *image = XGetImage(dpy, w, 0, 0 , wd1, ht1, AllPlanes, ZPixmap);
unsigned long rm = image->red_mask;
unsigned long gm = image->green_mask;
unsigned long bm = image->blue_mask;
Mat img(ht1, wd1, CV_8UC3); // OpenCV Mat object is initilaized
Mat scrap = imread("qr.jpg");//(wid, ht, CV_8UC3);
resize(scrap, img, img.size(), CV_INTER_AREA);
for (int x = 0; x < wd1; x++)
for (int y = 0; y < ht1 ; y++)
{
unsigned long pixel = XGetPixel(image,x,y);
Vec3b color = img.at<Vec3b>(Point(x,y));
pixel = 65536 * color[2] + 256 * color[1] + color[0];
XPutPixel(image, x, y, pixel);
}
namedWindow("QR", CV_WINDOW_NORMAL);
imshow("QR", img);
GC gc = XCreateGC(dpy, w, 0, NIL);
XPutImage(dpy, w, gc, image, 0, 0, 0, 0, wd1, ht1);
waitKey(0);
return 0;
}
How do I load images in the background?
What I tend to use the ThreadPool.QueueUserWorkItem
to load the image, then when the operation completes I call back to the UI thread using the thread-safe Dispatcher
object. The image source is not thread safe, you will have to use something like the JpegBitmapDecoder
, there is also a PngBitmapDecoder
.
For Example:
public Window()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(LoadImage,
"http://z.about.com/d/animatedtv/1/0/1/m/simp2006_HomerArmsCrossed_f.jpg");
}
public void LoadImage(object uri)
{
var decoder = new JpegBitmapDecoder(new Uri(uri.ToString()), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
decoder.Frames[0].Freeze();
this.Dispatcher.Invoke(DispatcherPriority.Send, new Action<ImageSource>(SetImage), decoder.Frames[0]);
}
public void SetImage(ImageSource source)
{
this.BackgroundImage.Source = source;
}
Can the full image of an X11 window be grab using the window id with the window is covered by another
Quoth the fine manual:
If the window has backing-store, the backing-store contents are returned for regions of the window that are obscured by noninferior windows. If the window does not have backing-store, the returned contents of such obscured regions are undefined.
So you need to set up the BackingStore window attribute.
Set bitmap background the same pixels as the bitmap behind it - Windows API
The comments have given enough detailed information, all you have to do is use TransparentBlt.
Minimal code example:
#include <Windows.h>
#pragma comment(lib,"Msimg32.lib")
using namespace std;
HBITMAP hBitmap;
HBITMAP hBitmap3;
void LoadMyImage(void) {
hBitmap = (HBITMAP)LoadImage(NULL, L"C:\\Users\\xx\\Desktop\\PGUfb.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hBitmap == NULL) {
MessageBox(NULL, L"Error while loading image", L"Error", MB_OK | MB_ICONERROR);
}
}
void LoadMyImage3(void) {
hBitmap3 = (HBITMAP)LoadImage(NULL, L"C:\\Users\\xx\\Desktop\\QZLMJ.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hBitmap3 == NULL) {
MessageBox(NULL, L"Error while loading image", L"Error", MB_OK | MB_ICONERROR);
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC memdc;
HDC dcSkin;
HBITMAP hOldMemBmp;
HBITMAP hOldSkinBmp;
switch (message)
{
case WM_CREATE:
{
LoadMyImage();
LoadMyImage3();
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// TODO: Add any drawing code that uses hdc here...
memdc = CreateCompatibleDC(hdc);
dcSkin = CreateCompatibleDC(hdc);
hOldMemBmp = (HBITMAP)SelectObject(memdc, hBitmap);
hOldSkinBmp = (HBITMAP)SelectObject(dcSkin, hBitmap3);
TransparentBlt(memdc, 200, 100, 38,38, dcSkin, 0, 0, 38, 38, RGB(255, 255, 255));
BitBlt(hdc, 0, 0, 988, 562, memdc, 0, 0, SRCCOPY);
DeleteObject(hOldSkinBmp);
DeleteObject(hOldMemBmp);
DeleteDC(memdc);
DeleteDC(dcSkin);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(hBitmap);
DeleteObject(hBitmap3);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
};
HINSTANCE hinst;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
HWND hwnd;
hinst = GetModuleHandle(NULL);
// create a window class:
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hinst;
wc.lpszClassName = L"win32";
// register class with operating system:
RegisterClass(&wc);
// create and show window:
hwnd = CreateWindow(L"win32", L"My program", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0, 0, 1000, 800, NULL, NULL, hinst, NULL);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, SW_SHOW);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Debug:
Related Topics
Why Do My Sfinae Expressions No Longer Work with Gcc 8.2
Import Nested Classes into Namespace - C++
Can This MACro Be Converted to a Function
What Is Double Evaluation and Why Should It Be Avoided
Move the String Out of a Std::Ostringstream
Passing a Non-Copyable Closure Object to Std::Function Parameter
Static Initialization and Destruction of a Static Library's Globals Not Happening with G++
Template Assignment Operator Overloading Mystery
Why Is It Not Possible to Overload the Ternary Operator
Random Numbers for Multiple Threads
Use Static_Assert to Check Types Passed to MACro
How to Calculate Offset of a Class Member at Compile Time
C++ Makefile on Linux with Multiple *.Cpp Files
Are End+1 Iterators for Std::String Allowed
How to Use a Constexpr Value in a Lambda Without Capturing It
Linux, Need Accurate Program Timing. Scheduler Wake Up Program
Forcing a Constant Expression to Be Evaluated During Compile-Time