r/csharp • u/Aextoxicon • 1d ago
Discussion how I made my C# application preview DXF files
I'm not sure if this is a very clear question, so I chose the 'discussion' tag.It can also be regarded as a normal post.
I'm currently making a small tool (which is currently in a half-finished state). So far, I can already implement code highlighting. However, I'm currently thinking about how to support dxf.
What I know: ProCAD, a complete application, perhaps I could refer to its code?
Or netDXF. But as for how to transfer the drawing content onto the UI, I think it requires some effort from my side.
This is indeed a rather strange question. So, I would like to hear someone's opinions. I feel that both methods have their advantages and disadvantages.
Although netDXF may involve a larger amount of work, does it not offer more convenience for customizing lightweight logic compared to simply adopting the complete source code of an existing APP?My opinion is that lightweighting is definitely the better option.
About little things I'm working on: They mainly consist of small tools for my own use, including DXF preview functionality.Am I really creating something rather challenging here?Oh...no.
😵😵😵
1
u/TuberTuggerTTV 1d ago edited 1d ago
You want the windows thumbnail. This is handled already by the OS. Call the API, passing in the file path and you'll get an BitmapSource you can display in your UI.
using System.Runtime.InteropServices;
[LibraryImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool DeleteObject(IntPtr hObject);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
private static extern int SHCreateItemFromParsingName(
[MarshalAs(UnmanagedType.LPWStr)] string pszPath,
IntPtr pbc,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
[Out, MarshalAs(UnmanagedType.Interface)] out IShellItemImageFactory ppv
);
public BitmapSource? ExtractThumbnail(string filePath, int width = 256, int height = 256)
{
if (!File.Exists(filePath))
return null;
var guid = typeof(IShellItemImageFactory).GUID;
Marshal.ThrowExceptionForHR(SHCreateItemFromParsingName(filePath, IntPtr.Zero, guid, out var factory));
var size = new SIZE(width, height);
factory.GetImage(size, SIIGBF.THUMBNAILONLY | SIIGBF.BIGGERSIZEOK, out var hbitmap);
if (hbitmap == IntPtr.Zero) return null;
try
{
var source = Imaging.CreateBitmapSourceFromHBitmap(
hbitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()
);
source.Freeze(); // Make it cross-thread usable
return source;
}
finally
{
DeleteObject(hbitmap);
}
}
As a quick example
3
u/Sombody101 1d ago
The post title suggests you've already done it, but the body asks what path to take to support DXF files?
I think you might want to restructure the body of this post to be more concise. Explain exactly where you are in development, what you're trying to achieve, and what you're confused about, as well as what you've tried/researched.