srknclk55
Aşağıdaki Yapı İşinizi Görür Umarım. Chatgpt O1 pro ile kodlanmıştır.
Aşağıda, Windows Forms kullanarak bir ImageList nesnesine resimlerin yüklenip, bir PictureBox içinde slayt gösterisi şeklinde görüntülenmesini sağlayan tam teşekküllü örnek kodu bulabilirsiniz. Bu örnekte;
- buttonLoad: Seçilen klasörden resimleri ImageList’e yükler
- buttonNext: Sıradaki resmi gösterir
- buttonPrevious: Önceki resmi gösterir
- buttonReset: İlk resme döner
- buttonClose: Uygulamayı kapatır
Form Tasarımında Gerekli Kontroller:
- Bir PictureBox (örn.
pictureBox1
)
- Beş adet Button (örn.
buttonLoad
, buttonNext
, buttonPrevious
, buttonReset
, buttonClose
)
Aşağıdaki kod örneğinde, resimler kullanıcının seçtiği klasörden okunur. (Not: ImageList, yüklenen resimleri ImageList.ImageSize boyutuna göre ölçeklendirir. Büyük boyutlu resimlerde orijinal boyutların korunmasını istiyorsanız, bunun yerine List<Image> kullanmak daha uygun olabilir.)
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace SlaytGosterisi
{
public partial class Form1 : Form
{
// Resimleri saklamak için ImageList nesnesi
private ImageList imageList = new ImageList();
// Şu anki resmin indeksi
private int currentImageIndex = 0;
public Form1()
{
InitializeComponent();
// İhtiyaca göre ImageList’in ImageSize değerini ayarlayabilirsiniz.
// Örneğin, PictureBox boyutuna uyumlu olsun:
imageList.ImageSize = new Size(pictureBox1.Width, pictureBox1.Height);
// PictureBox ayarları
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
// Resimleri yüklemek için "Load Images" butonunun tıklama olayı
private void buttonLoad_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
fbd.Description = "Resimlerin bulunduğu klasörü seçin";
if (fbd.ShowDialog() == DialogResult.OK)
{
string selectedPath = fbd.SelectedPath;
// Desteklenen resim uzantıları
string[] imageExtensions = new string[] { ".jpg", ".jpeg", ".png", ".bmp", ".gif" };
imageList.Images.Clear(); // Önceki resimleri temizle
// Klasördeki dosyaları tara
foreach (string file in Directory.GetFiles(selectedPath))
{
if (Array.Exists(imageExtensions, ext => file.EndsWith(ext, StringComparison.OrdinalIgnoreCase)))
{
try
{
// Resmi dosyadan oku ve ImageList'e ekle
Image img = Image.FromFile(file);
imageList.Images.Add(img);
}
catch (Exception ex)
{
MessageBox.Show("Resim yüklenirken hata oluştu: " + ex.Message);
}
}
}
currentImageIndex = 0;
if (imageList.Images.Count > 0)
{
pictureBox1.Image = imageList.Images[currentImageIndex];
}
else
{
MessageBox.Show("Seçilen klasörde geçerli resim bulunamadı.");
}
}
}
}
// Sıradaki resmi göster (Next)
private void buttonNext_Click(object sender, EventArgs e)
{
if (imageList.Images.Count > 0)
{
currentImageIndex = (currentImageIndex + 1) % imageList.Images.Count;
pictureBox1.Image = imageList.Images[currentImageIndex];
}
}
// Önceki resmi göster (Previous)
private void buttonPrevious_Click(object sender, EventArgs e)
{
if (imageList.Images.Count > 0)
{
// Negatif indeksi önlemek için mod alma işlemi:
currentImageIndex = (currentImageIndex - 1 + imageList.Images.Count) % imageList.Images.Count;
pictureBox1.Image = imageList.Images[currentImageIndex];
}
}
// İlk resme dön (Reset)
private void buttonReset_Click(object sender, EventArgs e)
{
if (imageList.Images.Count > 0)
{
currentImageIndex = 0;
pictureBox1.Image = imageList.Images[currentImageIndex];
}
}
// Uygulamayı kapat (Close)
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Kod Açıklamaları:
ImageList Tanımlaması:
imageList
nesnesi, yüklenen resimleri saklamak için kullanılır.
imageList.ImageSize
ile resimlerin gösterim boyutunu ayarlayabilirsiniz. Bu örnekte PictureBox’ın genişliği ve yüksekliği kullanılmıştır.
Resim Yükleme (buttonLoad_Click):
FolderBrowserDialog
ile kullanıcıdan resimlerin bulunduğu klasör seçilir.
- Seçilen klasördeki dosyalar, belirlenen uzantılara göre filtrelenir.
- Her resim
Image.FromFile()
ile okunur ve ImageList’e eklenir.
Navigasyon Butonları:
- Next (buttonNext_Click):
currentImageIndex
her tıklamada 1 artırılır. Listenin sonuna geldiğinde başa döner.
- Previous (buttonPrevious_Click):
currentImageIndex
azaltılır; negatif indekste listenin son elemanı gösterilir.
- Reset (buttonReset_Click): İndeks sıfırlanır ve ilk resim gösterilir.
Kapatma Butonu (buttonClose_Click):
- Form kapatılarak uygulama sonlandırılır.
Bu örnek, bir buton aracılığıyla resimleri ImageList’e yükleyip, diğer butonlar sayesinde slayt gösterisi şeklinde resimleri PictureBox’da göstermenizi sağlar. Geliştirmeler yaparken hata kontrolü, bellek yönetimi veya resim boyutlandırma gibi konulara da dikkat edebilirsiniz.