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 next 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
Re: Paste Properly Scaled Images Into RichEdit [message #61148 is a reply to message #61147] Sun, 24 November 2024 21:55 Go to previous messageGo to next message
emve is currently offline  emve
Messages: 10
Registered: February 2022
Promising Member
Here is the image with border:

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


Re: Paste Properly Scaled Images Into RichEdit [message #61151 is a reply to message #61148] Mon, 25 November 2024 10:20 Go to previous messageGo to next message
emve is currently offline  emve
Messages: 10
Registered: February 2022
Promising Member
I'm just wondering where those numbers came from:

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

But anyway, someone fix the proper image pasting to RichEdit, please.

Michal


Re: Paste Properly Scaled Images Into RichEdit [message #61155 is a reply to message #61151] Tue, 26 November 2024 14:03 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14257
Registered: November 2005
Ultimate Member
emve wrote on Mon, 25 November 2024 10:20
I'm just wondering where those numbers came from:

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

But anyway, someone fix the proper image pasting to RichEdit, please.

Michal




TLDR: Nothing to fix, but I can add ScreenRichEdit

The final target of RichText is printer. So pixel values are sort of irrelevant. U++ has two coordinate systems (only...): Screen pixels and 600DPI laser printer "pixels" (these are called "DOTS" Smile. So what you see is an attempt to approximately convert screen image size to paper. As most displays are 96DPI, we get 600/96 ratio. And those 2000 values are just sanity limit. But also beware that those "dots" are then converted back, so e.g. changing this to 1/1 does not help.

What you want is possible, but needs some work. Adding to the qeueu, please remind me if nothing happens in 1-2 months.
Re: Paste Properly Scaled Images Into RichEdit [message #61156 is a reply to message #61155] Tue, 26 November 2024 17:05 Go to previous message
emve is currently offline  emve
Messages: 10
Registered: February 2022
Promising Member
mirek wrote on Tue, 26 November 2024 14:03
emve wrote on Mon, 25 November 2024 10:20
I'm just wondering where those numbers came from:

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

But anyway, someone fix the proper image pasting to RichEdit, please.

Michal




TLDR: Nothing to fix, but I can add ScreenRichEdit

The final target of RichText is printer. So pixel values are sort of irrelevant. U++ has two coordinate systems (only...): Screen pixels and 600DPI laser printer "pixels" (these are called "DOTS" Smile. So what you see is an attempt to approximately convert screen image size to paper. As most displays are 96DPI, we get 600/96 ratio. And those 2000 values are just sanity limit. But also beware that those "dots" are then converted back, so e.g. changing this to 1/1 does not help.

What you want is possible, but needs some work. Adding to the qeueu, please remind me if nothing happens in 1-2 months.


Ok, thank you for the clarification Mirek.
And yes, ScreenRichEdit will be welcome Smile.
Michal


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


Current Time: Mon May 12 01:06:27 CEST 2025

Total time taken to generate the page: 0.13188 seconds