mirror of
https://github.com/egmont11/NetworkDiagram.git
synced 2026-07-24 07:09:43 +02:00
Added export option for printers, but it needs some tuning.
This commit is contained in:
@@ -4,11 +4,10 @@
|
||||
Title="{DynamicResource ExportOptionsTitle}" Width="400" Height="180"
|
||||
WindowStartupLocation="CenterOwner" CanResize="False"
|
||||
Background="{DynamicResource BgWhite}">
|
||||
<StackPanel Margin="20" Spacing="20">
|
||||
<TextBlock Text="{DynamicResource ExportWhiteBgMsg}" TextWrapping="Wrap" HorizontalAlignment="Center" TextAlignment="Center"/>
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Spacing="20">
|
||||
<Button Content="Yes (White)" Click="White_Click" Width="100" Classes="ModernButtonStyle"/>
|
||||
<Button Content="No (Transparent)" Click="Transparent_Click" Width="140" Classes="SecondaryButtonStyle"/>
|
||||
</StackPanel>
|
||||
<StackPanel Margin="20" Spacing="10">
|
||||
<TextBlock Text="{DynamicResource ExportWhiteBgMsg}" TextWrapping="Wrap" HorizontalAlignment="Center" TextAlignment="Center" Margin="0,0,0,10"/>
|
||||
<Button Content="{DynamicResource ExportWhiteBtn}" Click="White_Click" HorizontalAlignment="Stretch" Classes="ModernButtonStyle"/>
|
||||
<Button Content="{DynamicResource ExportTransparentBtn}" Click="Transparent_Click" HorizontalAlignment="Stretch" Classes="SecondaryButtonStyle"/>
|
||||
<Button Content="{DynamicResource ExportBWBtn}" Click="BlackAndWhite_Click" HorizontalAlignment="Stretch" Classes="SecondaryButtonStyle"/>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
|
||||
@@ -3,6 +3,14 @@ using Avalonia.Interactivity;
|
||||
|
||||
namespace NetworkDiagram
|
||||
{
|
||||
public enum ExportOption
|
||||
{
|
||||
Cancel,
|
||||
Transparent,
|
||||
White,
|
||||
BlackAndWhite
|
||||
}
|
||||
|
||||
public partial class ExportOptionsWindow : Window
|
||||
{
|
||||
public ExportOptionsWindow()
|
||||
@@ -12,12 +20,17 @@ namespace NetworkDiagram
|
||||
|
||||
private void White_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Close(true);
|
||||
Close(ExportOption.White);
|
||||
}
|
||||
|
||||
private void Transparent_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Close(false);
|
||||
Close(ExportOption.Transparent);
|
||||
}
|
||||
|
||||
private void BlackAndWhite_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Close(ExportOption.BlackAndWhite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,10 @@
|
||||
<x:String x:Key="SaveDialogBtn">Uložit</x:String>
|
||||
|
||||
<!-- Messages -->
|
||||
<x:String x:Key="ExportWhiteBgMsg">Chcete bílé pozadí? (Ne = průhlednost)</x:String>
|
||||
<x:String x:Key="ExportWhiteBgMsg">Vyberte styl pozadí a barev:</x:String>
|
||||
<x:String x:Key="ExportWhiteBtn">Bílé pozadí</x:String>
|
||||
<x:String x:Key="ExportTransparentBtn">Průhledné pozadí</x:String>
|
||||
<x:String x:Key="ExportBWBtn">Pro tisk (šedotisk)</x:String>
|
||||
<x:String x:Key="ExportOptionsTitle">Možnosti exportu</x:String>
|
||||
<x:String x:Key="ExportSuccess">Export byl úspěšný!</x:String>
|
||||
<x:String x:Key="EmptyDiagramMsg">Diagram je prázdný.</x:String>
|
||||
|
||||
@@ -19,7 +19,10 @@
|
||||
<x:String x:Key="SaveDialogBtn">Save</x:String>
|
||||
|
||||
<!-- Messages -->
|
||||
<x:String x:Key="ExportWhiteBgMsg">Do you want a white background? (No will result in transparency)</x:String>
|
||||
<x:String x:Key="ExportWhiteBgMsg">Select export background and color style:</x:String>
|
||||
<x:String x:Key="ExportWhiteBtn">White Background</x:String>
|
||||
<x:String x:Key="ExportTransparentBtn">Transparent Background</x:String>
|
||||
<x:String x:Key="ExportBWBtn">Printer Friendly (Grayscale)</x:String>
|
||||
<x:String x:Key="ExportOptionsTitle">Export Options</x:String>
|
||||
<x:String x:Key="ExportSuccess">Export successful!</x:String>
|
||||
<x:String x:Key="EmptyDiagramMsg">Diagram is empty.</x:String>
|
||||
|
||||
@@ -18,6 +18,7 @@ using Avalonia.Media.Imaging;
|
||||
using Avalonia.Platform.Storage;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using System.Runtime.InteropServices;
|
||||
using NetworkDiagram.Models;
|
||||
using Avalonia.Controls.Templates;
|
||||
|
||||
@@ -739,8 +740,8 @@ namespace NetworkDiagram
|
||||
if (_currentDiagram.Devices.Count == 0) return;
|
||||
|
||||
var prompt = new ExportOptionsWindow();
|
||||
var result = await prompt.ShowDialog<bool?>(this);
|
||||
if (result == null) return;
|
||||
var result = await prompt.ShowDialog<ExportOption>(this);
|
||||
if (result == ExportOption.Cancel) return;
|
||||
|
||||
var topLevel = GetTopLevel(this);
|
||||
if (topLevel == null) return;
|
||||
@@ -766,7 +767,7 @@ namespace NetworkDiagram
|
||||
var tempCanvas = new Canvas {
|
||||
Width = width,
|
||||
Height = height,
|
||||
Background = result.Value ? Brushes.White : Brushes.Transparent
|
||||
Background = (result == ExportOption.White || result == ExportOption.BlackAndWhite) ? Brushes.White : Brushes.Transparent
|
||||
};
|
||||
|
||||
// Create a container to host temp canvas for rendering
|
||||
@@ -781,13 +782,22 @@ namespace NetworkDiagram
|
||||
var textDark = Application.Current!.FindResource("TextDark") as IBrush ?? Brushes.Black;
|
||||
var gridBlue = Application.Current!.FindResource("GridBlue") as IBrush;
|
||||
|
||||
bool isGrayscale = result == ExportOption.BlackAndWhite;
|
||||
if (isGrayscale)
|
||||
{
|
||||
primaryBlue = Brushes.Black;
|
||||
accentBlue = Brushes.Black;
|
||||
textDark = Brushes.Black;
|
||||
gridBlue = Brushes.Black;
|
||||
}
|
||||
|
||||
// 1. Draw connections
|
||||
foreach (var conn in _currentDiagram.Connections)
|
||||
{
|
||||
if (_connectionLines.TryGetValue(conn, out var line))
|
||||
{
|
||||
var tempLine = new Line {
|
||||
Stroke = line.Stroke,
|
||||
Stroke = isGrayscale ? Brushes.Black : line.Stroke,
|
||||
StrokeThickness = line.StrokeThickness,
|
||||
StrokeDashArray = line.StrokeDashArray,
|
||||
StartPoint = new Point(line.StartPoint.X - minX, line.StartPoint.Y - minY),
|
||||
@@ -808,7 +818,7 @@ namespace NetworkDiagram
|
||||
Padding = new Thickness(12),
|
||||
Background = Brushes.White,
|
||||
BorderBrush = gridBlue ?? Brushes.Blue,
|
||||
BorderThickness = new Thickness(1),
|
||||
BorderThickness = new Thickness(isGrayscale ? 2 : 1),
|
||||
MinWidth = 100,
|
||||
MinHeight = 80,
|
||||
MaxWidth = 180
|
||||
@@ -825,7 +835,7 @@ namespace NetworkDiagram
|
||||
var txt = new TextBlock {
|
||||
Text = device.Name,
|
||||
FontWeight = FontWeight.Bold,
|
||||
FontSize = 12,
|
||||
FontSize = isGrayscale ? 14 : 12,
|
||||
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
TextAlignment = TextAlignment.Center,
|
||||
@@ -839,13 +849,14 @@ namespace NetworkDiagram
|
||||
{
|
||||
var desc = new TextBlock {
|
||||
Text = device.Description,
|
||||
FontSize = 11,
|
||||
FontSize = isGrayscale ? 18 : 11,
|
||||
FontWeight = isGrayscale ? FontWeight.Bold : FontWeight.Normal,
|
||||
FontStyle = FontStyle.Italic,
|
||||
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
TextAlignment = TextAlignment.Center,
|
||||
Foreground = textDark,
|
||||
Opacity = 0.8
|
||||
Opacity = isGrayscale ? 1.0 : 0.8
|
||||
};
|
||||
stack.Children.Add(desc);
|
||||
}
|
||||
@@ -854,7 +865,8 @@ namespace NetworkDiagram
|
||||
{
|
||||
var tb = new TextBlock {
|
||||
Text = ip,
|
||||
FontSize = 10,
|
||||
FontSize = isGrayscale ? 14 : 10,
|
||||
FontWeight = isGrayscale ? FontWeight.Bold : FontWeight.Normal,
|
||||
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
Foreground = accentBlue
|
||||
@@ -876,10 +888,67 @@ namespace NetworkDiagram
|
||||
await Task.Delay(100);
|
||||
rtb.Render(container);
|
||||
|
||||
if (result == ExportOption.BlackAndWhite)
|
||||
{
|
||||
using var ms = new MemoryStream();
|
||||
rtb.Save(ms);
|
||||
ms.Position = 0;
|
||||
var wb = WriteableBitmap.Decode(ms);
|
||||
ApplyGrayscale(wb);
|
||||
using var stream = await file.OpenWriteAsync();
|
||||
wb.Save(stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var stream = await file.OpenWriteAsync();
|
||||
rtb.Save(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyGrayscale(WriteableBitmap bitmap)
|
||||
{
|
||||
using (var lockedBitmap = bitmap.Lock())
|
||||
{
|
||||
int totalBytes = lockedBitmap.RowBytes * lockedBitmap.Size.Height;
|
||||
byte[] buffer = new byte[totalBytes];
|
||||
Marshal.Copy(lockedBitmap.Address, buffer, 0, totalBytes);
|
||||
|
||||
for (int y = 0; y < lockedBitmap.Size.Height; y++)
|
||||
{
|
||||
int rowOffset = y * lockedBitmap.RowBytes;
|
||||
for (int x = 0; x < lockedBitmap.Size.Width; x++)
|
||||
{
|
||||
int i = rowOffset + x * 4;
|
||||
if (i + 3 >= totalBytes) continue;
|
||||
|
||||
byte b = buffer[i];
|
||||
byte g = buffer[i + 1];
|
||||
byte r = buffer[i + 2];
|
||||
byte a = buffer[i + 3];
|
||||
|
||||
if (a < 128)
|
||||
{
|
||||
// Transparent -> White
|
||||
buffer[i] = 255;
|
||||
buffer[i + 1] = 255;
|
||||
buffer[i + 2] = 255;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Standard luminance for grayscale
|
||||
byte val = (byte)(0.299 * r + 0.587 * g + 0.114 * b);
|
||||
buffer[i] = val;
|
||||
buffer[i + 1] = val;
|
||||
buffer[i + 2] = val;
|
||||
}
|
||||
buffer[i + 3] = 255; // Always opaque for printing
|
||||
}
|
||||
}
|
||||
|
||||
Marshal.Copy(buffer, 0, lockedBitmap.Address, totalBytes);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user