Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » RichText,QTF,RTF... » Paste Properly Scaled Images Into RichEdit
Paste Properly Scaled Images Into RichEdit [message #61147] Sun, 24 November 2024 21:50 Go to previous message
emve is currently offline  emve
Messages: 10
Registered: February 2022
Promising Member
Hi,
I'm evaluating RichEdit using this code in main.cpp:

#include <CtrlLib/CtrlLib.h>
#include <RichEdit/RichEdit.h>
using namespace Upp;

GUI_APP_MAIN
{
	RichText txt;
	{
		RichPara para;
		RichPara::Format fmt;
		(Font&)fmt = Monospace(50).Bold();
		para.Cat("", fmt);
		txt.Cat(para);
	}
	
    RichEdit e;
    e.ShowCodes(Null);	//hide enter marks
    e.Clear();
	
    HDC screen = GetDC(NULL); // Get device context for the screen
    int dpiX = GetDeviceCaps(screen, LOGPIXELSX); // Horizontal DPI
    int dpiY = GetDeviceCaps(screen, LOGPIXELSY); // Vertical DPI
    ReleaseDC(NULL, screen);	
    			
    e.Pick(pick(txt));
        
    TopWindow win;
    win.Add(e.SizePos());
    win.Run();
}

When I copy paste an image (I use GreenShot) into RichEdit,
the pasted image shown is bigger.
Here I copied (PrintScreen using GreenShot) top left corner
of the window and pasted it back into RichEdit:

index.php?t=getfile&id=7008&private=0

When I paste the image from file, the result is the same.

Can it be solved anyhow? I asked chatgpt Smile and
it kinda hallucinated this solution (as there is
apparently no WhenPaste event handler in RichEdit object):

class MyApp : public TopWindow {
    RichEdit richEdit;

public:
    MyApp() {
        Title("Adjust DPI for Pasted Image").Sizeable();

        // Intercept the paste action and adjust the image DPI
        richEdit.WhenPaste = [&]() {
            // Check if the clipboard contains an image
            if (ClipboardHas<Image>()) {
                Image img = ReadClipboardImage();
                if (!img.IsEmpty()) {
                    // Get system DPI
                    Size dpi = GetPrimaryScreenDPI(); // Get DPI of the primary screen
                    double scaleFactor = dpi.cx / 96.0; // Assuming 96 DPI as the base

                    // Scale the image
                    int newWidth = img.GetWidth() * scaleFactor;
                    int newHeight = img.GetHeight() * scaleFactor;
                    Image scaledImage = Rescale(img, newWidth, newHeight);

                    // Insert the scaled image into the RichEdit control
                    richEdit.PasteImage(scaledImage);
                }
            } else {
                // Default paste behavior for non-image content
                richEdit.Paste();
            }
        };

        Add(richEdit.SizePos());
    }
};


Michal
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: String EncodeHtml
Next Topic: Create PDF with custom graphics through QTF
Goto Forum:
  


Current Time: Sun May 11 07:52:29 CEST 2025

Total time taken to generate the page: 0.03129 seconds