DOBON.NETプログラミング道掲示板

■35279 / 6階層)  RichTextBoxのテキストをpictureBOXへ
□投稿者/ KOZ 一般人(11回)-(2022/12/08(Thu) 18:38:13)
  • アイコン
    2022/12/09(Fri) 04:27:45 編集(投稿者)
    
    ■No35275に返信(ま〜さんの記事)
    >>高 DPI 環境で実行している場合は、ズレる可能性がありますね。
    > まさにこれが原因でした。凄いです感激です。
    
    これ動きますか?
    ちょっと問題があって、画像を縮小するせいか、少しぼやけた感じになります。
    ,NET Framework 4.7 以上なら RichTextBox の DrawToBitmap を使ったほうがいいかもしれません。
    
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        <DllImport("User32.dll")>
        Public Shared Function LogicalToPhysicalPointForPerMonitorDPI(ByVal hwnd As IntPtr, ByRef point As Point) As Boolean
        End Function
    
        Private Shared Function LogicalToPhysicalRectangleForPerMonitorDPI(ByVal hwnd As IntPtr, ByVal r As Rectangle) As Rectangle
            Dim p1 As Point = r.Location
            Dim p2 As Point = New Point(r.Right, r.Bottom)
            LogicalToPhysicalPointForPerMonitorDPI(hwnd, p1)
            LogicalToPhysicalPointForPerMonitorDPI(hwnd, p2)
            Return Rectangle.FromLTRB(p1.X, p1.Y, p2.X, p2.Y)
        End Function
    
        Private Function LogicalToPhysicalRectangleForPerMonitorDPI(ByVal r As Rectangle) As Rectangle
            Return LogicalToPhysicalRectangleForPerMonitorDPI(Handle, r)
        End Function
    
        Public Sub New()
            InitializeComponent()
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            PictureBox1.Size = RichTextBox1.Size
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim rectangle1 = RectangleToScreen(RichTextBox1.Bounds)
            Dim rectangle2 = LogicalToPhysicalRectangleForPerMonitorDPI(rectangle1)
            Dim bmp = New Bitmap(rectangle2.Width, rectangle2.Height)
            Using g = Graphics.FromImage(bmp)
                g.CopyFromScreen(rectangle2.Location, Point.Empty, rectangle2.Size)
            End Using
            If PictureBox1.Image IsNot Nothing Then
                PictureBox1.Image.Dispose()
            End If
            PictureBox1.Image = bmp
        End Sub
    
    End Class
    

違反を報告
削除キー/

前の記事(元になった記事) 次の記事(この記事の返信)
←Re[5]: RichTextBoxのテキストをpictureBOXへ /ま〜 →Re[7]: RichTextBoxのテキストをpictureBOXへ /ま〜
 
上記関連ツリー

Nomalアイコン RichTextBoxのテキストをpictureBOXへ / ま〜 (22/12/05(Mon) 15:45) #35270
Nomalアイコン Re[1]: RichTextBoxのテキストをpictureBOXへ / KOZ (22/12/06(Tue) 00:29) #35271
  └Nomalアイコン Re[2]: RichTextBoxのテキストをpictureBOXへ / ま〜 (22/12/06(Tue) 12:52) #35272
    └Nomalアイコン Re[3]: RichTextBoxのテキストをpictureBOXへ / KOZ (22/12/06(Tue) 16:20) #35273
      └Nomalアイコン Re[4]: RichTextBoxのテキストをpictureBOXへ / 魔界の仮面弁士 (22/12/06(Tue) 16:51) #35274
        └Nomalアイコン Re[5]: RichTextBoxのテキストをpictureBOXへ / ま〜 (22/12/08(Thu) 14:56) #35275
          ├Nomalアイコン Re[6]: RichTextBoxのテキストをpictureBOXへ / 魔界の仮面弁士 (22/12/08(Thu) 15:22) #35276
          │└Nomalアイコン Re[7]: RichTextBoxのテキストをpictureBOXへ / 魔界の仮面弁士 (22/12/08(Thu) 15:25) #35277
          └Nomalアイコン RichTextBoxのテキストをpictureBOXへ / KOZ (22/12/08(Thu) 18:38) #35279 ←Now
            └Nomalアイコン Re[7]: RichTextBoxのテキストをpictureBOXへ / ま〜 (22/12/15(Thu) 13:11) #35280
              └Nomalアイコン Re[8]: RichTextBoxのテキストをpictureBOXへ / KOZ (22/12/15(Thu) 18:00) #35282
                └Nomalアイコン Re[9]: RichTextBoxのテキストをpictureBOXへ / ま〜 (22/12/19(Mon) 13:37) #35295
                  └Nomalアイコン Re[10]: RichTextBoxのテキストをpictureBOXへ / ま〜 (22/12/19(Mon) 13:45) #35296 解決み!

All 上記ツリーを一括表示 / 上記ツリーをトピック表示
 
上記の記事へ返信

Mode/  Pass/


- Child Tree -