Aufgabe:
Ein kleiner einfach Windows C# Code zum verkleinern eines Fotos oder Bildes auf ein kleineres Format
Betrifft: Windows Forms, C#. Funktioniert auch in .NET Core code, Resize Format, Größe ändern
Bitmap source_Bitmap = new Bitmap(input_Image_Path); Image outImage = new Bitmap(source_Bitmap, 100, 100); |
Komplettes Code Beispiel.
Hier wird ein Foto verkleinert. Dabei wird das Foto auf eine maximale Kantenlänge verkleinert.
public static void load_Photo_compressed_to_Clipboard(string input_Image_Path, int setLength) { //---------------< Image_resize() >--------------- Bitmap source_Bitmap = new Bitmap(input_Image_Path);
double dblWidth_origial = source_Bitmap.Width; double dblHeigth_origial = source_Bitmap.Height;
//< check orientation > bool IsOrientation_Horizontal = (dblWidth_origial > dblHeigth_origial) ? true : false; //</ check orientation >
int new_Height = 0; int new_Width = 0; double zoom_factor = 1; if (IsOrientation_Horizontal == true) { new_Width = setLength; zoom_factor = new_Width / dblWidth_origial; new_Height = (int)(dblHeigth_origial * zoom_factor); } else { new_Height = setLength; zoom_factor = new_Height / dblHeigth_origial; new_Width = (int)(dblWidth_origial * zoom_factor); }
Image outImage = new Bitmap(source_Bitmap, new_Width, new_Height); var t = new Thread((ThreadStart)(() => { Clipboard.SetImage(outImage); })); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join();
//---------------</ Image_resize() >--------------- } |
Dabei müssen folgende Namespaces verwendet werden
using System.Threading.Tasks; using System.Threading;
using System.Drawing; //*compress photo by windows using System.Drawing.Imaging; //*compress photo by windows using System.Drawing.Drawing2D; //*compress quality
|