Skip to content

Commit 4c096d4

Browse files
authored
Merge pull request #7166 from syncfusion-content/992782-ie
992782: Add UG Documentation for Fit to Width and Height
2 parents 5cbeb06 + e4548bd commit 4c096d4

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

blazor-toc.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,6 +3281,7 @@
32813281
<li> <a href="/blazor/image-editor/how-to/reset">Reset an image</a></li>
32823282
<li> <a href="/blazor/image-editor/how-to/clear-image">Clear an Image</a></li>
32833283
<li> <a href="/blazor/image-editor/how-to/render-dialog">Render Dialog in Image Editor</a></li>
3284+
<li> <a href="/blazor/image-editor/how-to/fit-to-width-and-height">Fit Image to Editor Width and Height</a></li>
32843285
</ul>
32853286
</li>
32863287
<li>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
![Blazor Image Editor clearing the image](../images/blazor-image-editor-fit-to-width-and-height.jpg)
63.1 KB
Loading

0 commit comments

Comments
 (0)