Page Index Toggle Pages: 1 [2]  Send TopicPrint
Very Hot Topic (More than 25 Replies) Change the measurement unit in Diagram (Read 6613 times)
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #15 - Mar 30th, 2020 at 3:18pm
Print Post  
private void ImageManipulation (string path)
{
diagram.MeasureUnit = GraphicsUnit.Millimeter;
ruler.Unit = RulerUnit.Millimeter;
var myImage = new BitmapImage (new Uri (path));

var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
var imageWidth = GraphicsUnit.Pixel.Convert (imageWidthPx, diagram.MeasureUnit);
var imageHeight = GraphicsUnit.Pixel.Convert (imageHeightPx, diagram.MeasureUnit);

imageNode = diagram.Factory.CreateShapeNode (0, 0, imageWidth, imageHeight);
imageNode.Image = myImage;
imageNode.Shape = Shapes.Rectangle;
imageNode.CustomDraw = CustomDraw.Additional;
imageNode.ImageAlign = ImageAlign.Fit;

var overlayNodeWidth = 50;
var overlayNodeHeight = 50;

overlayNode = diagram.Factory.CreateShapeNode (imageNode.Bounds.Right-overlayNodeWidth,
imageNode.Bounds.Bottom-overlayNodeHeight, overlayNodeWidth, overlayNodeHeight);
overlayNode.Shape = Shapes.Rectangle;
overlayNode.Brush = Brushes.Transparent;
overlayNode.Locked = true;
overlayNode.AttachTo (imageNode, AttachToNode.BottomRight);
}

I have set
diagram.MeasureUnit = GraphicsUnit.Millimeter as you said earlier;
ruler.Unit = RulerUnit.Millimeter;
How can graphics.MeasureUnit be set to Millimeter, please give me sample code.
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #16 - Mar 30th, 2020 at 3:35pm
Print Post  
What is the value of diagram.MeasureUnit at the time you're doing the conversion?
  

conversion.png ( 66 KB | 113 Downloads )
conversion.png
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #17 - Mar 30th, 2020 at 3:50pm
Print Post  
Hey, Lyubo. Smiley
  

3_30_41.png ( 247 KB | 116 Downloads )
3_30_41.png
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #18 - Mar 30th, 2020 at 4:01pm
Print Post  
This place is wrong:
         private void diagram_DrawNode(object sender, DrawNodeEventArgs e)
        {
            if (e.Node != imageNode)
                return;

            var graphics = e.Graphics;
            var clip = new CombinedGeometry(
                GeometryCombineMode.Exclude,
                new RectangleGeometry(imageNode.Bounds),
                new RectangleGeometry(overlayNode.Bounds));
            var translate = new TranslateTransform(-imageNode.Bounds.X, -imageNode.Bounds.Y);
            graphics.PushTransform(translate);
            graphics.PushClip(clip);
            graphics.DrawRectangle(new SolidColorBrush(Color.FromArgb(120, 0, 0, 0)), null, imageNode.Bounds);
            graphics.Pop();
            graphics.Pop();
        }
  

3_30_42.png ( 264 KB | 115 Downloads )
3_30_42.png
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #19 - Mar 31st, 2020 at 1:41am
Print Post  
Hey,Lyubo. Smiley
My current program is modified to have only this code to look for errors.
But still couldn't figure out the problem, the only possibility of error lies in this expression: var imageWidth = GraphicsUnit.Pixel.Convert (imageWidthPx, diagram.MeasureUnit);

MainWindow.xmal:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MeasurementUnit"
xmlns:diag="http://mindfusion.eu/diagramming/wpf" x:Class="MeasurementUnit.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>

<diag:Ruler Name="ruler" ShowIcon="False" FontSize="11" Grid.Row="1" Grid.Column="1" BorderBrush="Black">

<diag:Diagram x:Name="diagram"
AllowDrop="True"
VerticalAlignment="Top"
HorizontalAlignment="Left"
DefaultShape="RoundRect"
Behavior="Pan"
GridSizeX="15" GridSizeY="15" RestrictItemsToBounds="InsideOnly" RightButtonActions="None"
Bounds="-500,-500,2000,2000"
>
</diag:Diagram>
</diag:Ruler>
</Grid>
</Window>


MainWindow.xmal.cs:

using Microsoft.Win32;
using MindFusion.Diagramming.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MeasurementUnit
{
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
diagram.MeasureUnit = GraphicsUnit.Millimeter;
ruler.Unit = RulerUnit.Millimeter;
OpenFileDialog openfiledialog = new OpenFileDialog
{
Filter = "*.jpg;*.png;*.jpeg;*.bmp;*.gif|*.*"
};

if ((bool)openfiledialog.ShowDialog())
{
ImageManipulation(openfiledialog.FileName);
}
}
static ShapeNode imageNode;

private void ImageManipulation(string path)
{
var myImage = new BitmapImage(new Uri(path, UriKind.Relative));

var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
var imageWidth = GraphicsUnit.Pixel.Convert(imageWidthPx, diagram.MeasureUnit);
var imageHeight = GraphicsUnit.Pixel.Convert(imageHeightPx, diagram.MeasureUnit);

imageNode = diagram.Factory.CreateShapeNode(0, 0, imageWidth, imageHeight);
imageNode.Image = myImage;
imageNode.Shape = Shapes.Rectangle;
imageNode.CustomDraw = CustomDraw.Additional;
imageNode.ImageAlign = ImageAlign.Fit;
}
}
}

Figure 1 is the picture I tested, you can check.
  

1-20200330183807-100u.jpg ( 917 KB | 102 Downloads )
1-20200330183807-100u.jpg
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #20 - Mar 31st, 2020 at 1:45am
Print Post  
Supplement: Figure 2 is the result after program operation.
  

3_31_1.png ( 1063 KB | 110 Downloads )
3_31_1.png
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #21 - Mar 31st, 2020 at 5:21am
Print Post  
Hi,

Add the following method:
Code
Select All
private double GetScreenDpi()
{
    double dpi = 96;
    PresentationSource source = PresentationSource.FromVisual(this);

    if (source != null)
        dpi =  96.0 * source.CompositionTarget.TransformToDevice.M11;

    return dpi;
} 



and modify your current code with this:
Code
Select All
var imageWidthPx = myImage.PixelWidth * GetScreenDpi() / myImage.DpiX;
var imageHeightPx = myImage.PixelHeight * GetScreenDpi() / myImage.DpiY; 



It should display properly after these changes.

Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #22 - Mar 31st, 2020 at 5:37am
Print Post  
Yes, it worked. Thank you, Lyubo. Smiley
Can you explain to me what this method does: GetScreenDpi ()
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #23 - Mar 31st, 2020 at 5:59am
Print Post  
It returns the device-dependent screen DPI setting.
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #24 - Mar 31st, 2020 at 6:04am
Print Post  
Great, you taught me another point of knowledge Wink
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #25 - Mar 31st, 2020 at 6:06am
Print Post  
Lyubo, I currently have several questions posted on the new topic, waiting for you to teach me Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send TopicPrint