รบกวน คนเก่ง แก้ code ให้ที คะ
ดีค่ะทุกท่าน ช่วยแก้ code ให้นู๋หน่อยค่ะ มันแปลงภาพแล้วขนาดไฟล์มันไม่ยอมลด ไม่รู้ทำยังไง รบกวนทุกท่านด้วยนะคะ เวลาแปลงไฟล์ icon มันกลับเพิ่มขนาดไฟล์ แต่รูปเท่าเดิมงงมากคะ
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Text; using System.Windows.Forms;
namespace ImageConverter { public partial class Form1 : Form { // ฟังก์ชั่นประกาศตัวแปล string CurrentFile; Image img;
public Form1() { InitializeComponent(); }
// ฟังก์ชั่นโชว์ไฟล์ นามสกุล ที่สามารถเลือกและแปลงไฟล์ได้ // รูปแบบนามสกุลที่สามารถแปลงไฟล์ได้ private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Title = "Open Image File"; openFileDialog1.Filter = "Bitmap Files|*.bmp" + "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" + "|PNG Files|*.png "; openFileDialog1.DefaultExt = "bmp"; openFileDialog1.FilterIndex = 1; openFileDialog1.FileName = ""; openFileDialog1.ShowDialog();
if (openFileDialog1.FileName == "") return;
CurrentFile = openFileDialog1.FileName.ToString();
img = Image.FromFile(openFileDialog1.FileName); pictureBox1.Image = img; }
// ฟังก์ชั่น การปิด โปรแกรม private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); }
// ขั้นตอนการแปลงไฟล์ของแต่ละนามสกุล;
// ฟังก์ชั่นแปลงไฟล์เป็น Bitmap private void bitmapToolStripMenuItem_Click(object sender, EventArgs e) { string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile); newName = newName + ".bmp";
try { img.Save(newName, ImageFormat.Bmp); } catch { MessageBox.Show("Failed to save image to bitmap.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); }
// การแปลงไฟล์เป็น GIF private void gIFFileToolStripMenuItem_Click(object sender, EventArgs e) { string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile); newName = newName + ".gif";
try { img.Save(newName, ImageFormat.Gif); } catch { MessageBox.Show("Failed to save image to GIF format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); }
// การแปลงไฟล์เป็น Icon private void iconFileToolStripMenuItem_Click(object sender, EventArgs e) { string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile); newName = newName + ".ico";
try { img.Save(newName, ImageFormat.Icon); } catch { MessageBox.Show("Failed to save image to ICO format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); }
// การแปลงไฟล์เป็น JPEG private void jPEGFileToolStripMenuItem_Click(object sender, EventArgs e) { string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile); newName = newName + ".jpg";
try { img.Save(newName, ImageFormat.Jpeg); } catch { MessageBox.Show("Failed to save image to JPEG format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); }
// การแปลงไฟล์เป็น PNG private void pNGFileToolStripMenuItem_Click(object sender, EventArgs e) { string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile); newName = newName + ".png";
try { img.Save(newName, ImageFormat.Png); } catch { MessageBox.Show("Failed to save image to PNG format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
MessageBox.Show("Image file saved to " + newName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e) {
}
private void pictureBox1_Click(object sender, EventArgs e) {
}
private void Form1_Load(object sender, EventArgs e) {
} } }
|