|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Fit to Width and Height with Blazor Image Editor | Syncfusion |
| 4 | +description: Learn how to fit to width and height in the Blazor Image Editor component for Blazor Server and WebAssembly applications. |
| 5 | +platform: Blazor |
| 6 | +control: Image Editor |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Fit Image to Editor Width and Height |
| 11 | + |
| 12 | +The Image Editor's [ZoomAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.ImageEditor.SfImageEditor.html#Syncfusion_Blazor_ImageEditor_SfImageEditor_ZoomAsync_System_Double_Syncfusion_Blazor_ImageEditor_ImageEditorPoint_) method to fit an image to the editor by width or height. Programmatically increase the zoom level until the image dimension matches the editor container's width or height. |
| 13 | + |
| 14 | +This example demonstrates scenarios that include buttons for fitting the image to its width (Fit Width) or height (Fit Height). |
| 15 | + |
| 16 | +```cshtml |
| 17 | +@using Syncfusion.Blazor.ImageEditor |
| 18 | +@using Syncfusion.Blazor.Buttons |
| 19 | +<SfImageEditor @ref="ImageEditor" Height="400" Width="500"> |
| 20 | + <ImageEditorEvents Created="OpenAsync"></ImageEditorEvents> |
| 21 | +</SfImageEditor> |
| 22 | +<div style="padding-bottom: 15px"> |
| 23 | + <SfButton OnClick="FitWidth">Fit Width</SfButton> |
| 24 | + <SfButton OnClick="FitHeight">Fit Height</SfButton> |
| 25 | +</div> |
| 26 | +@code { |
| 27 | + SfImageEditor ImageEditor; |
| 28 | + private async void OpenAsync() |
| 29 | + { |
| 30 | + await ImageEditor.OpenAsync("https://ej2.syncfusion.com/react/demos/src/image-editor/images/bridge.png"); |
| 31 | + } |
| 32 | + private async void FitWidth() |
| 33 | + { |
| 34 | + await ImageEditor.ZoomAsync(1); // Reset zoom to original size before applying Fit Width |
| 35 | + ImageDimension Dimension = await ImageEditor.GetImageDimensionAsync(); |
| 36 | + double ContainerWidth = Convert.ToDouble(ImageEditor.Width); |
| 37 | + if (Dimension is not null) |
| 38 | + { |
| 39 | + double ImageWidth = Dimension.Width; |
| 40 | + double OldWidth = Dimension.Width; |
| 41 | + double ZoomFactor = 0.1; |
| 42 | + double NewZoom; |
| 43 | + NewZoom = 1; |
| 44 | + while (ImageWidth < ContainerWidth) |
| 45 | + { |
| 46 | + NewZoom++; |
| 47 | + ImageWidth = OldWidth + (OldWidth * ZoomFactor); |
| 48 | + ZoomFactor += 0.1; |
| 49 | + } |
| 50 | + await ImageEditor.ZoomAsync(NewZoom); |
| 51 | + } |
| 52 | + } |
| 53 | + private async void FitHeight() |
| 54 | + { |
| 55 | + await ImageEditor.ZoomAsync(1); // Reset zoom to original size before applying Fit Height |
| 56 | + ImageDimension Dimension = await ImageEditor.GetImageDimensionAsync(); |
| 57 | + double ContainerHeight = Convert.ToDouble(ImageEditor.Height); |
| 58 | + if (Dimension is not null) |
| 59 | + { |
| 60 | + double ImageHeight = Dimension.Height; |
| 61 | + double OldHeight = Dimension.Height; |
| 62 | + double ZoomFactor = 0.1; |
| 63 | + double NewZoom = 1; |
| 64 | + while (ImageHeight < ContainerHeight) |
| 65 | + { |
| 66 | + NewZoom++; |
| 67 | + ImageHeight = OldHeight + (OldHeight * ZoomFactor); |
| 68 | + ZoomFactor += 0.1; |
| 69 | + } |
| 70 | + await ImageEditor.ZoomAsync(NewZoom); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
0 commit comments