clipboard image
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [textures] example - clipboard image
//!
//! Zig-API translation of examples/textures/textures_clipboard_image.c (raylib 6.2).
//! Example contributed by Maicon Santana (@maiconpintoabreu) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2026 Maicon Santana (@maiconpintoabreu)
const rl = @import("raylib");
const max_texture_collection = 20;
const TextureCollection = struct {
texture: rl.Texture2D,
position: rl.Vector2,
};
pub fn main() void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [textures] example - clipboard image");
defer rl.closeWindow(); // Close window and OpenGL context
var collection: [max_texture_collection]TextureCollection = undefined;
var collection_count: usize = 0;
defer for (collection[0..collection_count]) |item| rl.unloadTexture(item.texture);
rl.setTargetFPS(60);
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
if (rl.isKeyPressed(.r)) { // Reset image collection
// Unload textures to avoid memory leaks
for (collection[0..collection_count]) |item| rl.unloadTexture(item.texture);
collection_count = 0;
}
if (rl.isKeyDown(.left_control) and rl.isKeyPressed(.v) and
(collection_count < max_texture_collection))
{
const image = rl.getClipboardImage();
if (rl.isImageValid(image)) {
defer rl.unloadImage(image);
if (rl.loadTextureFromImage(image)) |texture| {
collection[collection_count] = .{ .texture = texture, .position = rl.getMousePosition() };
collection_count += 1;
} else |_| {}
} else rl.traceLog(.info, "IMAGE: Could not retrieve image from clipboard", .{});
}
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
for (collection[0..collection_count]) |item| {
if (rl.isTextureValid(item.texture)) {
const width: f32 = @floatFromInt(item.texture.width);
const height: f32 = @floatFromInt(item.texture.height);
rl.drawTexturePro(
item.texture,
.{ .x = 0, .y = 0, .width = width, .height = height },
.{ .x = item.position.x, .y = item.position.y, .width = width, .height = height },
.{ .x = width * 0.5, .y = height * 0.5 },
0.0,
.white,
);
}
}
rl.drawRectangle(0, 0, screen_width, 40, .black);
rl.drawText("Clipboard Image - Ctrl+V to Paste and R to Reset ", 120, 10, 20, .light_gray);
}
}/*******************************************************************************************
*
* raylib [textures] example - clipboard image
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 6.0, last time updated with raylib 6.0
*
* Example contributed by Maicon Santana (@maiconpintoabreu) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2026 Maicon Santana (@maiconpintoabreu)
*
********************************************************************************************/
#include "raylib.h"
#define MAX_TEXTURE_COLLECTION 20
typedef struct TextureCollection {
Texture2D texture;
Vector2 position;
} TextureCollection;
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - clipboard image");
TextureCollection collection[MAX_TEXTURE_COLLECTION] = { 0 };
int currentCollectionIndex = 0;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_R)) // Reset image collection
{
// Unload textures to avoid memory leaks
for (int i = 0; i < currentCollectionIndex; i++) UnloadTexture(collection[i].texture);
currentCollectionIndex = 0;
}
if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V) &&
(currentCollectionIndex < MAX_TEXTURE_COLLECTION))
{
Image image = GetClipboardImage();
if (IsImageValid(image))
{
collection[currentCollectionIndex].texture = LoadTextureFromImage(image);
collection[currentCollectionIndex].position = GetMousePosition();
currentCollectionIndex++;
UnloadImage(image);
}
else TraceLog(LOG_INFO, "IMAGE: Could not retrieve image from clipboard");
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < currentCollectionIndex; i++)
{
if (IsTextureValid(collection[i].texture))
{
DrawTexturePro(collection[i].texture,
(Rectangle){0,0,(float)collection[i].texture.width, (float)collection[i].texture.height},
(Rectangle){collection[i].position.x,collection[i].position.y, (float)collection[i].texture.width, (float)collection[i].texture.height},
(Vector2){collection[i].texture.width*0.5f, collection[i].texture.height*0.5f},
0.0f, WHITE);
}
}
DrawRectangle(0, 0, screenWidth, 40, BLACK);
DrawText("Clipboard Image - Ctrl+V to Paste and R to Reset ", 120, 10, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
for (int i = 0; i < currentCollectionIndex; i++)
{
UnloadTexture(collection[i].texture);
}
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.