Resmi Pencerede Göstermek Nasıl Yapılır – C#
Resmi pencerede göstermek için C# platformunda tıklayınca yeni bir form açarak picturebox nesnemizdeki resmi o formun background image özelliğine atayacağız. Daha önceki yazımızda picturebox nesnemize sürükle+bırak (drag+drop) olayı ile bir uygulama yapmıştık. O uygulama üzerine bu uygulamayı geliştireceğiz. (Bkzn: Resim Boyutunu Öğrenme)
Bir önceki yazımızdaki aynı form yapısını kullanarak aşağıdaki kodu entegre edin. Formun üzerin bir picturebox nesnesi koyun. Bir label koyarak sürükle bırak yaptığımız resmin boyutunu bu labele yazacağız.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | [crayon-607391510af23682973838 inline="true" ]// www.kaizen40.com using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); pictureBox1.DragEnter += PicMevcut_DragEnter; pictureBox1.DragDrop += PicMevcut_DragDrop; } private void Form1_Load(object sender, EventArgs e) { pictureBox1.AllowDrop = true; } private void PicMevcut_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.All; } else { e.Effect = DragDropEffects.None; } } private void PicMevcut_DragDrop(object sender, DragEventArgs e) { string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); for (int i = 0; i < s.Length; i++) { var iSize = new FileInfo(s[i]).Length; // 1Mb dan ufak resim ekle if ((iSize / 1204) <= 1024) { label1.Text = (iSize / 1024).ToString() + " Kb"; label1.ForeColor = Color.Green; pictureBox1.ImageLocation = s[i]; } else { label1.Text = "Dosya Boyutu Büyük!"; label1.ForeColor = Color.Red; } } } private void pictureBox1_Click(object sender, EventArgs e) { try { // yeni bir pencerede resmi aç var FormImage = new Form { Width = 800, Height = 600, //BackgroundImage = new Bitmap(PicMevcut.ImageLocation), BackgroundImage = pictureBox1.Image, BackgroundImageLayout = ImageLayout.Stretch }; FormImage.Show(); } catch (Exception) { MessageBox.Show("Resim seçili değil!"); } } } } |
Picturebox nesnemize tıklayınca resmimiz yeni pencerede oluşmaktadır. Bu pecerenin boyutunu kod yapısı üzerinden değiştirebilirsiniz. Aynı zamanda yüklediğiniz resmi form boyutlarına strect image yaparak en boy oranını değiştirebilirsiniz.
