1
.NET / Re: Previews or Thumbnails
« Last post by cmwade77 on Today at 04:50:17 PM »I think I must be missing something here, here is the code I have:
Code - C#: [Select]
- public static Bitmap? Thumbnail(this Database db)
- {
- try
- {
- Bitmap? thumbnail = db.ThumbnailBitmap;
- if (thumbnail == null)
- {
- db.SafeSave(true);
- if (thumbnail == null)
- {
- return PlaceholderThumbnail();
- }
- }
- return thumbnail;
- }
- catch (System.Exception ex)
- {
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nError extracting thumbnail: {ex.Message}");
- return null;
- }
- }
- private static Bitmap PlaceholderThumbnail()
- {
- // Create a simple placeholder thumbnail
- using (Graphics g = Graphics.FromImage(thumbnail))
- {
- g.Clear(Color.Gray);
- string text = "No Preview";
- SizeF textSize = g.MeasureString(text, font);
- // Calculate the position to center the text
- float x = (thumbnail.Width - textSize.Width) / 2;
- float y = (thumbnail.Height - textSize.Height) / 2;
- }
- return thumbnail;
- }
- public static bool SafeSave(this Database db, bool KeepDate = false)
- {
- string filenameToMatch = db.Filename;
- try
- {
- DateTime Old = File.GetLastWriteTime(filenameToMatch);
- if (!filenameToMatch.IsFileLocked())
- {
- db.SaveAs(filenameToMatch, true, DwgVersion.Current, null); // Overwrite the original file
- return true;
- }
- else
- {
- Document activeDoc = Application.DocumentManager.MdiActiveDocument;
- if (string.Equals(filenameToMatch, activeDoc.Database.Filename, StringComparison.OrdinalIgnoreCase))
- {
- activeDoc.SendStringToExecute("_.qsave\n", false, false, false);
- return true;
- }
- }
- if (KeepDate)
- {
- File.SetLastWriteTime(filenameToMatch, Old);
- }
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- Application.ShowAlertDialog($"Error saving file: {ex.Message}");
- }
- return false;
- }