cart: empty [ Login ]

How to set bitmap dpi using ImageExporter

1 
Yusuke
8/8/2023 3:00 AM
Hi Wout &c, How can I use ImageExporter.CreateAutoSizedBitmap below to output an image with a specified DPI? Does GraphicsConfig's DotsPerInch have no effect on image resolution? The purpose is to insert the generated image into an Excel file and print it at a resolution of 300DPI.
C# Code:
var widthxx = bounds3D.Corner2.X - bounds3D.Corner1.X; var heightxx = bounds3D.Corner2.Y - bounds3D.Corner1.Y; var matrix4D = WW.Cad.Base.DxfUtil.GetScaleTransform( new Point3D(bounds3D.Corner1.X, bounds3D.Corner1.Y, 0), new Point3D(bounds3D.Corner2.X, bounds3D.Corner2.Y, 0), new Point3D(bounds3D.Center.X, bounds3D.Center.Y, 0), new Point3D(0, 0, 0), new Point3D(widthxx, heightxx, 0), new Point3D(widthxx / 2, heightxx / 2, 0)); var scaleTransform = CreateScaleTransform(bounds3D, dxfScale, width, height); matrix4D = scaleTransform * matrix4D; var graphicsConfig = GetGraphicsConfig(judanTemplateInfoEntity.dpi); SixLabors.ImageSharp.GraphicsOptions renderOptions = new() { Antialias = true }; WW.Cad.Drawing.NetCore.NetCoreGraphics3D graphics3D = new(graphicsConfig, renderOptions) { Transform = Matrix4D.Identity }; graphics3D.BoundingBox(bounds3D); graphics3D.CreateDrawables(dxfModel); // DXF->BMP 変換(投影) var bitmap = ImageExporter.CreateAutoSizedBitmap<Rgba32>(graphics3D, ImageRenderOptions.Default, matrix4D, ArgbColors.White); // 拡大・縮小を行った行列で投影する。 using (Stream stream = File.Create(outImagePath.FilePath)) { ImageExporter.EncodeImageToPng(bitmap, stream); } --------------------------- private GraphicsConfig GetGraphicsConfig(int dpi) { WW.Cad.Drawing.GraphicsConfig graphicsConfig = new WW.Cad.Drawing.GraphicsConfig(ArgbColors.White) { FixedForegroundColor = ArgbColors.Black, DotsPerInch = dpi, CorrectColorForBackgroundColor = false, ApplyLineType = true, DisplayLineWeight = false, DrawImages = true, TryDrawingTextAsText = true }; return graphicsConfig; }
Yusuke
Wout
8/8/2023 9:12 AM
Hi, The GraphicsConfig.DotsPerInch currently does not set the bitmap's dpi (it will change this in the next release). It currently only affects the calculation of the number of pixels for a certain line width, so it should indeed ideally by equal to the bitmap's dpi. Try the following just after you create the bitmap:
C# Code:
// Set dpi. bitmap.Metadata.HorizontalResolution = dpi; bitmap.Metadata.VerticalResolution = dpi;
- Wout
Yusuke
8/8/2023 10:22 AM
Hi, thank you. I would like to purchase the Windows version. Yusuke
Wout
8/8/2023 10:28 AM
Hi Yusuke, You can order the 6.0 for windows version here. - Wout
Yusuke
8/8/2023 10:52 AM
Hi, And I'm sorry many times. Let me ask you one more question. Do I need .NET Desktop Runtime 6.0 to use the Windows version? Desktop Runtime 6.0 is not installed because it is assumed to run on WindowsServer. Yusuke
Wout
8/8/2023 11:02 AM
Hi, I'm not entirely sure, there is an option to build a self-contained application, there's a bit more info here. I haven't tried this myself, so you will have to try this on your end to see if it works. - Wout
1