Das Laden der Grafik erfolgt über einen eindachen OpenDialog, die Grafik wird in einem HiddenImage abgelegt, damit die Skalierung per Trackbar zugriff auf das Originalbild hat.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace UploadThumbnail
{
public partial class FormImageToThumbnain : Form
{
private int OrgWidth { get; set; }
private int OrgHeight { get; set; }
public FormImageToThumbnain()
{
InitializeComponent();
}
private void btUpload_Click(object sender, EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
opendialog.Filter = "Image files(*.jpg; *.jpeg; *.gif)| *.jpg; *.jpeg; *.gif";
if (opendialog.ShowDialog() == DialogResult.OK)
{
Bitmap img = new Bitmap(opendialog.FileName);
OrgWidth = img.Width;
OrgHeight = img.Height;
pbUpload.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
opendialog.RestoreDirectory = true;
CreateThumb();
}
}
private void tBar_Scroll(object sender, EventArgs e)
{
label1.Text = "Prozent:" + tBar.Value;
if (OrgWidth > 0 && OrgHeight > 0)
CreateThumb();
}
private void CreateThumb()
{
Image imag = pbUpload.Image;
decimal inPrc = tBar.Value ;
int widthPerc = (int)Math.Abs(OrgWidth * inPrc/100);
int heightPerc = (int)Math.Abs(OrgHeight * inPrc/100);
pbThumbnail.Image = imag.GetThumbnailImage(widthPerc, heightPerc, null, new IntPtr());
pbThumbnail.Width = widthPerc;
pbThumbnail.Height = heightPerc;
lblOutput.Text = string.Format("\nOriginalgröße Width:{0} Height:{1}", OrgWidth, OrgHeight);
lblOutput.Text += string.Format("\nThumbgröße Width:{0} Height:{1}", widthPerc, heightPerc);
this.Height = heightPerc + 100;
this.Width = widthPerc+250;
}
}
}