[wpf]Imaging.RenderTargetBitmap สร้างภาพผิดตำแหน่งครับ

Last post 25 Feb 2010 3:01 by Richter. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 18 Feb 2010 14:08

    [wpf]Imaging.RenderTargetBitmap สร้างภาพผิดตำแหน่งครับ

    ตัวโค๊ดที่ใช้ในการสร้างภาพ
           <ex.Extension()> Function Image(ByVal Data As sw.FrameworkElement, _
            Optional ByVal DpiX# = 96, Optional ByVal DpiY# = 96) _
            As swm.Imaging.RenderTargetBitmap
                With Data
                    Image = New swm.Imaging.RenderTargetBitmap( _
                            .ActualWidth, .ActualHeight, _
                            DpiX, DpiY, swm.PixelFormats.Pbgra32)
                    Image.Render(Data)
                End With
            End Function

    ผมกำลังทำให้การ Drag&Drop ในโปรแกรม มีไอคอนเหมือนติดเมาส์ไปด้วย
    เหมือนใน Explorer ซึ่งการทดสอบตำแหน่งต่างๆ ไม่มีปัญหา

    Nomal state.


    While mouse on.


    Test drag position.


    แต่พอใช้รูปแทนปุบ ก็มีปัญหาดังนี้


    Code ของ event นี้ครับ

    Private Sub ClickLD(ByVal sender As Object, _
    ByVal e As sw.Input.MouseButtonEventArgs)
        
        'C.F ^ Sender == Dircast(sender,Frameworkelement)
        Dim Frame = C.F ^ sender
        Dim Ex = Frame.Image
        Dim EF = RealPosition(Frame, e)
    
        With MoveIcon
            .Height = Ex.Height
            .Width = Ex.Width
            .Source = Ex
            .Margin = New Double() {EF.X, EF.Y, 0, 0}.Margin
        End With
        
        Dim FF = New swc.Grid With {.Height = Ex.Height, .Width = Ex.Width, _
                    .Margin = New Double() {EF.X, EF.Y, 0, 0}.Margin}
    
        'FF.Children.Add(MoveIcon)
        FF.Background = "FFffffff".Color.Solid
    
        MoveIcon.IsHitTestVisible = False
        FF.IsHitTestVisible = False
        AutoSet.AsForm = New PackOf.Frame + MoveIcon + FF
    
        'Use FF insert MoveIcon for test.
        ReItem = MoveIcon
        MoveOnPanel.Children.Add(ReItem)
    
        With ReItem
            .DataContext = EF 
            .CaptureMouse()
            AddHandler .PreviewMouseMove, AddressOf Move
            AddHandler .MouseLeftButtonUp, AddressOf ClickLT
        End With
    End Sub
    
  • 24 Feb 2010 12:12 In reply to

    Re: [wpf]Imaging.RenderTargetBitmap สร้างภาพผิดตำแหน่งครับ

    เอ จะเกี่ยวกับ Margin ของ Data ป่ะครับ
  • 25 Feb 2010 3:01 In reply to

    Re: [wpf]Imaging.RenderTargetBitmap สร้างภาพผิดตำแหน่งครับ

    มีสาเหตุบางประการที่ทำให้ RenderTargetBitmap ทำงานแล้วได้ผลไม่ตรงกับที่ผมหวังไว้
    (ตอนที่ทดลองใช้ก่อนสร้างเป็นฟังค์ชั่นเก็บเข้าเฟรมเวิร์ค ก็สร้างภาพ FrameworkElement ได้สมบรูณ์ดีครับ)
    น่าจะเป็นที่ Margin อย่างที่คุณ nantcom ว่าไว้

    แต่สุดท้ายเมื่อทางตรงไปไม่ได้ เลยจัดการลัดป่าละเมาะข้างทางดังนี้ละครับ

    Protected Overrides Sub InterupActive _
    (ByVal Input As sw.FrameworkElement, _
     ByVal e As sw.Input.MouseButtonEventArgs)
    
        Dim EF = RealMargin(Input, MoveOnPanel)
    
        Dim MoveIcon = New swc.Grid
        With MoveIcon
            .Height = Input.Height
            .Width = Input.Width
            .Margin = New Double() {EF.X, EF.Y, 0, 0}.Margin
            .Background = New swm.VisualBrush(Input)
            .IsHitTestVisible = False
        End With
        
        AutoSet.AsForm = New PackOf.Frame + MoveIcon
    
        ReItem = MoveIcon
        MoveOnPanel.Children.Add(ReItem)
    End Sub
    
    หน้าตาอาจดูแปลกๆจากเดิมเนื่องจากมีคลาสตระกูล Drag อยู่หลายตัว
    เลยต้องจับเข้าไว้ Namespace เดียวกันแล้วสร้าง Static Class ดึง Core ออกมา
    จากนั้นค่อยเอาส่วนขยายต่างๆ Inherit Core เข้าไปแล้วเพิ่มเติม
Page 1 of 1 (3 items)