110 lines
4.2 KiB
C#
110 lines
4.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Drawing;
|
|||
|
|
using System.Drawing.Imaging;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Windows;
|
|||
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|||
|
|
using CommunityToolkit.Mvvm.Input;
|
|||
|
|
using Microsoft.Win32;
|
|||
|
|
using System.Runtime.InteropServices;
|
|||
|
|
using System.Windows.Interop;
|
|||
|
|
using System.Windows.Media.Imaging;
|
|||
|
|
|
|||
|
|
namespace RevitLess
|
|||
|
|
{
|
|||
|
|
public partial class MainViewModel : ObservableObject
|
|||
|
|
{
|
|||
|
|
[ObservableProperty]
|
|||
|
|
public partial List<RevitFileAssist.FamilyTypeDefinition> SymbolTypes { get; set; }
|
|||
|
|
[ObservableProperty]
|
|||
|
|
public partial RevitFileAssist.FamilyTypeDefinition SelectedSymbolType { get; set; }
|
|||
|
|
[ObservableProperty]
|
|||
|
|
public partial System.Windows.Media.ImageSource PreviewImage { get; set; }
|
|||
|
|
|
|||
|
|
[ObservableProperty]
|
|||
|
|
public partial string FileInfo { get; set; }
|
|||
|
|
|
|||
|
|
[DllImport("gdi32.dll")]
|
|||
|
|
static extern bool DeleteObject(IntPtr hObject);
|
|||
|
|
|
|||
|
|
public static System.Windows.Media.ImageSource ToImageSource(System.Drawing.Image image)
|
|||
|
|
{
|
|||
|
|
if (image == null)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 始终创建一个与输入 image 无关的 Bitmap 副本(深拷贝),
|
|||
|
|
// 然后从该副本获取 HBITMAP 并创建 WPF BitmapSource。
|
|||
|
|
// 这样可以确保底层 GDI+ 资源在返回后仍然有效(WPF 会复制像素数据)。
|
|||
|
|
using (System.Drawing.Bitmap clone = new System.Drawing.Bitmap(image))
|
|||
|
|
{
|
|||
|
|
IntPtr hBitmap = clone.GetHbitmap();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
System.Windows.Media.Imaging.BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
|
|||
|
|
hBitmap,
|
|||
|
|
IntPtr.Zero,
|
|||
|
|
Int32Rect.Empty,
|
|||
|
|
BitmapSizeOptions.FromEmptyOptions());
|
|||
|
|
bitmapSource.Freeze();
|
|||
|
|
return bitmapSource;
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// 释放由 GetHbitmap 创建的 GDI 对象
|
|||
|
|
DeleteObject(hBitmap);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static System.Windows.Media.ImageSource ConvertImageToImageSource(System.Drawing.Image image)
|
|||
|
|
{
|
|||
|
|
using (var ms = new MemoryStream())
|
|||
|
|
{
|
|||
|
|
image.Save(ms, ImageFormat.Png);
|
|||
|
|
ms.Seek(0, SeekOrigin.Begin);
|
|||
|
|
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
|
|||
|
|
bitmap.BeginInit();
|
|||
|
|
bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
|
|||
|
|
bitmap.StreamSource = ms;
|
|||
|
|
bitmap.EndInit();
|
|||
|
|
bitmap.Freeze();
|
|||
|
|
return bitmap;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void OpenFile()
|
|||
|
|
{
|
|||
|
|
OpenFileDialog dialog = new OpenFileDialog();
|
|||
|
|
dialog.Filter = "Revit文件|*.rvt;*.rfa";
|
|||
|
|
if (dialog.ShowDialog() == true)
|
|||
|
|
{
|
|||
|
|
Storage storage = new Storage(dialog.FileName);
|
|||
|
|
//var image = storage.ThumbnailImage.GetPreviewAsImage();
|
|||
|
|
var info = new RevitFileAssist(dialog.FileName);
|
|||
|
|
PreviewImage = ToImageSource(info.PreviewImage);
|
|||
|
|
SymbolTypes = info.SymbolTypes;
|
|||
|
|
//FileInfo = info.FileInfo.ToString();
|
|||
|
|
//FileInfo = storage.BasicInfo.ToString();
|
|||
|
|
//SaveImage(dialog.FileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private static void SaveImage(string filePath)
|
|||
|
|
{
|
|||
|
|
var fi = new System.IO.FileInfo(filePath);
|
|||
|
|
Storage storage = new Storage(fi.FullName);
|
|||
|
|
var b = storage.ThumbnailImage.GetPreviewAsImage();
|
|||
|
|
MessageBox.Show(storage.BasicInfo.ToString());
|
|||
|
|
|
|||
|
|
//Bitmap bm = new Bitmap(b.Width, b.Height, PixelFormat.Format24bppRgb);
|
|||
|
|
//Graphics g = Graphics.FromImage(bm);
|
|||
|
|
//g.DrawImage(b, new Point(0, 0));
|
|||
|
|
//g.Dispose();
|
|||
|
|
|
|||
|
|
//bm.Save(Path.Combine(fi.Directory.FullName, "_Images", fi.Name.Remove(fi.Name.Length - 4) + ".png"), System.Drawing.Imaging.ImageFormat.Png);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|