Getting Properties of an image[VC++.NET]
Getting Properties of an Image
Language: VC++.NET
Author: Aamir Mustafa
Introduction:
This simple article will show how to get image properties. In this article I will load a photo and then its property will be shown. it will display all the properties of an image like name, image type, image size, its resolution.
How to show property:
Create an empty project and add following controls on the form.
| Control | Properties |
| Form | Name=Form1 Text= Properties Width=554 Height=224 |
| Button | Name= btnLoad Text= Load Photo |
| ListView | Name= lstProperties Width=329 Height=149 Scrollable=False Font Style=Bold Font Size=8 ForeColor=Coral |
| PictureBox | Name= pic Width=117 Height=149 BorderStyle=FixedSingle SizeMode=StretchImage |
After doing that go to view code and add following code below in public area of class Form1.
public:
Bitmap ^bitmapImage;
String ^imageFile;
After that add following just below the above code
Here we are initializing the List View so when first our program runs so we can see two columns, one for Property and second for Property value. Both columns will be added when we first launch our program. Both columns have equal width. Type of View will be detail so we can show properties in detail like a table and we will set Grid Lines to true so that we can show grid lines. We will write following code in Form1 constructor.
Form1(void)
{
InitializeComponent();
lstProperties->View=View::Details;
lstProperties->Columns->Add("Property",Convert::ToInt32(lstProperties->Width*50/100));
lstProperties->Columns->Add("Value",Convert::ToInt32(lstProperties->Width*50/100));
lstProperties->GridLines=true;
}
Now write a function called GetProperties which gets the properties of an image and shows them in list view.
private:
void GetProperties()
{
lstProperties->Items->Clear();
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
}
After that add following code to the load button’s click event. This opens a dialog to load picture from the local disk.
private: System::Void btnLoad_Click(System::Object^ sender, System::EventArgs^ e)
{
FileDialog ^fileDialog = gcnew OpenFileDialog();
fileDialog->Filter = "JPG (*.jpg)|*.jpg| Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpeg)|*.jpeg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif|All Files (*.*)|*.*";
fileDialog->ShowDialog(this);
pic->SizeMode = PictureBoxSizeMode::StretchImage;
imageFile = fileDialog->FileName;
if (imageFile == System::String::Empty)
return;
pic->Image = Image::FromFile(imageFile);
bitmapImage = gcnew Bitmap(imageFile);
GetProperties();
}
Getting Properties of an image[VB.NET]
Getting Properties of an Image
Language: VB.NET
Author: Aamir Mustafa
Introduction:
This simple article will show how to get image properties. In this article I will load a photo and then its property will be shown. it will display all the properties of an image like name, image type, image size, its resolution.
How to show property:
Create an empty project and add following controls on the form.
| Control | Properties |
| Form | Name=Form1 Text= Properties Width=554 Height=224 |
| Button | Name= btnLoad Text= Load Photo |
| ListView | Name= lstProperties Width=329 Height=149 Scrollable=False Font Style=Bold Font Size=8 ForeColor=Coral |
| PictureBox | Name= pic Width=117 Height=149 BorderStyle=FixedSingle SizeMode=StretchImage |
After doing that go to view code and add following code below Public Class Form1
Dim bitmapImage As Bitmap
Dim imageFile As String
After that add following just below the above code
Here we are initializing the List View so when first our program runs so we can see two columns, one for Property and second for Property value.
Public Sub New()
InitializeComponent()
lstProperties.View = View.Details
lstProperties.View = View.Details
lstProperties.Columns.Add("Property", Convert.ToInt32(lstProperties.Width * 50 \ 100))
lstProperties.Columns.Add("Value", Convert.ToInt32(lstProperties.Width * 45 \ 100))
lstProperties.GridLines = True
End Sub
Now write a function called GetProperties which gets the properties of an image and shows them in list view.
Private Sub GetProperties()
lstProperties.Items.Clear() ' clear the list view
lstProperties.Items.Add(New ListViewItem(New String() {"Name", System.IO.Path.GetFileNameWithoutExtension(imageFile)})) 'this will show name of image but not with extension
lstProperties.Items.Add(New ListViewItem(New String() {"Type", System.IO.Path.GetExtension(imageFile)})) 'this will show image type like JPEG
lstProperties.Items.Add(New ListViewItem(New String() {"Size", New System.IO.FileInfo(imageFile).Length.ToString() & " Bytes"})) 'this will show image size in bytes
lstProperties.Items.Add(New ListViewItem(New String() {"Width", pic.Width.ToString() & " Pixels"})) 'this will show image width
lstProperties.Items.Add(New ListViewItem(New String() {"Height", pic.Height.ToString() & " Pixels"})) 'this will show image height
lstProperties.Items.Add(New ListViewItem(New String() {"Vertical Resolution", bitmapImage.VerticalResolution.ToString() & " Pixels/Inch"})) 'this will show Vertical Resolution
lstProperties.Items.Add(New ListViewItem(New String() {"Horizontal Resolution", bitmapImage.HorizontalResolution.ToString() & " Pixels/Inch"})) 'this will show Horizontal Resolution
lstProperties.Items.Add(New ListViewItem(New String() {"Pixel Format", bitmapImage.PixelFormat.ToString()})) 'this will show pixel format.
lstProperties.Items.Add(New ListViewItem(New String() {"Palette Flags", bitmapImage.Palette.Flags.ToString()})) 'this will show pallete flags
End Sub
After that add following code to the load button’s click event. This opens a dialog to load picture from the local disk.
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim fileDialog As FileDialog = New OpenFileDialog()
fileDialog.Filter = "JPG (*.jpg)|*.jpg| Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpeg)|*.jpeg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif|All Files (*.*)|*.*"
fileDialog.ShowDialog(Me)
pic.SizeMode = PictureBoxSizeMode.StretchImage
imageFile = fileDialog.FileName
If imageFile = String.Empty Then
Return
End If
pic.Image = Image.FromFile(imageFile)
bitmapImage = New Bitmap(imageFile)
GetProperties() ‘call GetProperties method
End Sub
Note:
If you want to get size of the image in Kilo Bytes then you can do it as follow
System.IO.FileInfo(imageFile).Length.ToString()\1024 & lstProperties.Items.Add(New ListViewItem(New String() {"Size", New" KB"})) 'this will show image size in kilo bytes
Draw a background with a rotating color gradient in Visual Basic .NET
| ||||||||
| When the form's Timer fires, the event handler invalidates the drawing area PictureBox. The PictureBox's Paint event handler makes a LinearGradientBrush. The rectangle the gradient covers is slightly larger than the PictureBox's client rectangle. The brush uses the angle m_Theta to determine the gradient's angle. The program fills the PictureBox with the brush. It then updates m_Theta so the gradient rotates next time. Finally the program draws some text over the colored background. | ||||||||
Private m_Theta As Single = 0 |
