Hi all,
i want to export image for drafting view, but i export the image as below code get image with some part is cut-off in image.
i try to ExportRange.VisibleRegionOfCurrentView, get the image is blank.
please suggest me how to solve my problem.
private void ExportDraftingViewAsImage(ViewDrafting draftingView)
{
try
{
if (SaveImageCheckBox.IsChecked == true)
{
if (draftingView == null || !(draftingView is ViewDrafting))
{
WpfMessageBox.Show("Drafting view is not valid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// Validate image dimensions
if (!int.TryParse(ImageHeightTextBox.Text, out int imageHeight) ||
!int.TryParse(ImageWidthTextBox.Text, out int imageWidth) ||
imageHeight <= 0 || imageWidth <= 0)
{
WpfMessageBox.Show("Invalid image dimensions.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// Validate base file path
string baseFilePath = FilePathTextBox.Text;
if (string.IsNullOrWhiteSpace(baseFilePath))
{
WpfMessageBox.Show("Invalid base file path.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// Generate a unique filename with a timestamp
string uniqueFileName = $"{DateTime.Now:yyyyMMdd_HHmmssfff}.png";
string fullPath = System.IO.Path.Combine(baseFilePath, uniqueFileName);
// Set the view to active outside of any transaction
_uidoc.ActiveView = draftingView;
// Temporarily deactivate crop box and zoom settings inside a transaction
bool originalCropBoxActive = draftingView.CropBoxActive;
using (Transaction trans = new Transaction(_doc, "Prepare Drafting View for Export"))
{
try
{
trans.Start();
// Disable the crop box to capture the full view
draftingView.CropBoxActive = false;
_doc.Regenerate(); // Ensure document is up-to-date after changes
trans.Commit();
}
catch (Exception ex)
{
trans.RollBack();
WpfMessageBox.Show($"An error occurred while preparing the drafting view: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
// Now, perform the image export (this doesn't require a transaction)
try
{
// Set up the export options
ImageExportOptions options = new ImageExportOptions
{
ExportRange = ExportRange.CurrentView, // Export the entire current view
HLRandWFViewsFileType = ImageFileType.PNG,
PixelSize = Math.Max(imageWidth, imageHeight), // Ensure positive value
ImageResolution = ImageResolution.DPI_300,
FilePath = fullPath,
ZoomType = ZoomFitType.FitToPage,
FitDirection = FitDirectionType.Horizontal,
ShadowViewsFileType = ImageFileType.PNG
};
// Set the view to export
options.SetViewsAndSheets(new List<ElementId> { draftingView.Id });
// Export the image
_doc.ExportImage(options);
WpfMessageBox.Show($"Image exported successfully to {fullPath}.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
WpfMessageBox.Show($"An error occurred during image export: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
// Finally, restore the original crop box setting inside a transaction
using (Transaction trans = new Transaction(_doc, "Restore Drafting View Settings"))
{
try
{
trans.Start();
draftingView.CropBoxActive = originalCropBoxActive; // Restore the original setting
_doc.Regenerate();
trans.Commit();
}
catch (Exception ex)
{
trans.RollBack();
WpfMessageBox.Show($"An error occurred while restoring the drafting view settings: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
else
{
WpfMessageBox.Show("Image export is not enabled. Please check 'Save Image' to enable export.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
WpfMessageBox.Show($"An error occurred during image export: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}