ali raza

C#, .Net ,Asp.net and my thoughts

Category Archives: Windows form

How to resize image in .net

use follwing Code change image path at load event method for downloading Code

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim WorkingDirectory As String = "c:\AliPicture"

'create a image object containing a verticel photograph
Dim imgPhotoVert As Image = Image.FromFile(WorkingDirectory + "\pravs-j-the-first-step.jpg")
Dim imgPhoto As Image = Nothing
imgPhoto = FixedSize(imgPhotoVert, 100, 100)
imgPhoto.Save(WorkingDirectory + "\imageresize_3.jpg", ImageFormat.Jpeg)
imgPhoto.Dispose()

End Sub

Private Shared Function FixedSize(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0

Dim nPercent As Single = 0
Dim nPercentW As Single = 0
Dim nPercentH As Single = 0

nPercentW = (CSng(Width) / CSng(sourceWidth))
nPercentH = (CSng(Height) / CSng(sourceHeight))

'if we have to pad the height pad both the top and the bottom
'with the difference between the scaled height and the desired height
If nPercentH < nPercentW Then
nPercent = nPercentH
destX = CInt(((Width - (sourceWidth * nPercent)) / 2))
Else
nPercent = nPercentW
destY = CInt(((Height - (sourceHeight * nPercent)) / 2))
End If

Dim destWidth As Integer = CInt((sourceWidth * nPercent))
Dim destHeight As Integer = CInt((sourceHeight * nPercent))

Dim bmPhoto As New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.Clear(Color.Red)
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

grPhoto.Dispose()
Return bmPhoto
End Function

End Class

Saving and Restoring Form Location

A common requirement for a form is to remember its last location. Usually, this information
is stored in the registry. The code that follows shows a helper class that automatically stores
information about a form’s size and position using a key based on the name of a form.
public class FormPositionHelper
{
public static string RegPath = @”Software\App\”;
public static void SaveSize(System.Windows.Forms.Form frm)
{
// Create or retrieve a reference to a key where the settings will be stored.
RegistryKey key;
key = Registry.LocalMachine.CreateSubKey(RegPath + frm.Name);
key.SetValue(“Height”, frm.Height);
key.SetValue(“Width”, frm.Width);
key.SetValue(“Left”, frm.Left);
key.SetValue(“Top”, frm.Top);
}

public static void SetSize(System.Windows.Forms.Form frm)
{
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(RegPath + frm.Name);
if (key != null)
{
frm.Height = (int)key.GetValue(“Height”);
frm.Width = (int)key.GetValue(“Width”);
frm.Left = (int)key.GetValue(“Left”;
frm.Top = (int)key.GetValue(“Top”);
}
}
}
To use this class in a form, you call the SaveSize() method when the form is closing:

private void MyForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
FormPositionHelper.SaveSize(this);
}
and call the SetSize() method when the form is first opened:

private void MyForm_Load(object sender, System.EventArgs e)
{
FormPositionHelper.SetSize(this);
}

How to fix the size of windows form at different monitor resolution

Some time developer/coder have to choose appropriate location of windows form on the screen, But limitation comes with the size and resolution of monitor. The solution is used Screen Class which gives right gives you working area of screen of monitor. Then Form load Event will look like this

private void dynamicSizeForm_Load(System.Object sender, System.EventArgse)
{
Screen scr = Screen.PrimaryScreen;
this.Left = (scr.WorkingArea.Width – this.Width) / 2;
this.Top = (scr.WorkingArea.Height – this.Height) / 2;
}
You can use above code to fix the location and size of windows form when it loads

Screen.PrimaryScreen 

Returns a Rectangle structure that represents the bounds
of the display area for the current screen, minus the space
taken for the taskbar and any other docked windows.