CefImage
Header: cef_image.h
Category: Core APIs
Overview
Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.
CefImage
Source File: include/cef_image.h
Process / Thread Context: Any browser process thread (per class comment: "The methods of this class can be called on any browser process thread.")
Purpose: USER-CALLED library interface storing a single image at multiple scale factors. Used for browser-window icons, drag images (returned from CefDragData::GetImage()), and any other UI surfaces requiring per-DPI raster data. All representations should share the same DIP size.
Methods
static CefRefPtr<CefImage> CreateImage()
Return Value: A new empty CefImage.
Usage Instruction: Call once, then add representations via AddBitmap/AddPNG/AddJPEG.
Threading Constraint: Any browser process thread.
bool IsEmpty()
Return Value: True if image has no representations.
Threading Constraint: Threading constraint not specified in source.
bool IsSame(CefRefPtr<CefImage> that)
Return Value: True if this image and that share underlying storage (also true if both are empty).
Threading Constraint: Threading constraint not specified in source.
bool AddBitmap(float scale_factor, int pixel_width, int pixel_height, cef_color_type_t color_type, cef_alpha_type_t alpha_type, const void* pixel_data, size_t pixel_data_size)
Parameters:
- Only 32-bit RGBA/BGRA supported;
pixel_datalength must equalpixel_width * pixel_height * 4.color_type/alpha_typespecify the pixel format.
Return Value: True on success.
Usage Instruction: Use for pre-decoded bitmaps at known scale factors.
Threading Constraint: Any browser process thread.
bool AddPNG(float scale_factor, const void* png_data, size_t png_data_size)
bool AddJPEG(float scale_factor, const void* jpeg_data, size_t jpeg_data_size)
size_t GetWidth()
Return Value: Width in DIP units.
Threading Constraint: Threading constraint not specified in source.
size_t GetHeight()
Return Value: Height in DIP units.
Threading Constraint: Threading constraint not specified in source.
bool HasRepresentation(float scale_factor)
Return Value: True if a representation exists for the given scale factor.
Threading Constraint: Threading constraint not specified in source.
bool RemoveRepresentation(float scale_factor)
Return Value: Removes the representation. True on success.
Threading Constraint: Threading constraint not specified in source.
bool GetRepresentationInfo(float scale_factor, float& actual_scale_factor, int& pixel_width, int& pixel_height)
Return Value: Returns info for the representation nearest to scale_factor. True on success.
Threading Constraint: Threading constraint not specified in source.
CefRefPtr<CefBinaryValue> GetAsBitmap(float scale_factor, cef_color_type_t color_type, cef_alpha_type_t alpha_type, int& pixel_width, int& pixel_height)
CefRefPtr<CefBinaryValue> GetAsPNG(float scale_factor, bool with_transparency, int& pixel_width, int& pixel_height)
CefRefPtr<CefBinaryValue> GetAsJPEG(float scale_factor, int quality, int& pixel_width, int& pixel_height)
Usage Example
// Build a 1x / 2x icon from PNG buffers:
CefRefPtr<CefImage> img = CefImage::CreateImage();
img->AddPNG(1.0f, png_1x_data, png_1x_size);
img->AddPNG(2.0f, png_2x_data, png_2x_size);
// Apply as the browser window icon (Views framework):
// window->SetImage(img);
// or via platform-specific host APIs that accept CefImage.
// Export back to PNG (with transparency) at the closest scale:
int pw = 0, ph = 0;
CefRefPtr<CefBinaryValue> png = img->GetAsPNG(2.0f, true, pw, ph);
// Inspect drag image (from CefDragHandler::OnDragEnter):
if (dragData->HasImage()) {
CefRefPtr<CefImage> dimg = dragData->GetImage();
CefPoint hotspot = dragData->GetImageHotspot();
size_t w = dimg->GetWidth();
size_t h = dimg->GetHeight();
}