Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Em làm bộ lọc Pixelate để làm mờ ảnh mà làm hoài không được ạ :(

    Tình hình là em làm bộ lọc ảnh Pixelate để làm mờ 1 bức ảnh với việc gom các điểm ảnh có m x m điểm ảnh thành 1 điểm ảnh lớn có cùng màu (quy định bởi 1 thanh trượt từ 1 tới 10) > mục đích làm nhòe ảnh.

    Em sài hàm theo hướng dẫn ở link nè ạ:
    http:/otes.ericwillis.com/2009/11/...e-with-csharp/

    Nhưng khi gọi hàm thì có vẻ nó không ổn, nó chả mờ tý nào cả (khi chọn số điểm ảnh gom bằng cách lăn thanh trượt, và ấn nút biến đổi).

    Form của em nó có dạng nè ạ:



    Còn đây là project cho các pro tiện giúp em ạ [IMG]images/smilies/biggrin.png[/IMG]
    http://www.mediafire.com/?2k8n8isax5uhp59

    Đây là code của em ạ [IMG]images/smilies/biggrin.png[/IMG]

    Mã:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace demo_boloc
    {
        public partial class frmBiendoiloc : Form
        {
            public static int biendoi = 1;
            public frmBiendoiloc()
            {
                InitializeComponent();
            }
    
            private void frmBiendoiloc_Load(object sender, EventArgs e)
            {
                ptbAnhgoc.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + @"/temp/xuly.bmp");
                ptbAnhgoc.SizeMode = PictureBoxSizeMode.CenterImage;
                Image img = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + @"/temp/xuly.bmp");
                lblKichthuoc.Text = img.Size.ToString() + "; ";
                string duongdan = System.IO.Directory.GetCurrentDirectory() + @"/temp/xuly.bmp";
                FileInfo thongtinfile = new FileInfo(duongdan);
                lblKichco.Text = thongtinfile.Length.ToString() + "Byte";
            }
    
            private void btnLuuAnh_Click(object sender, EventArgs e)
            {
                saveFileDialog1.Title = "Lưu file ảnh định dạng bmp";
                saveFileDialog1.InitialDirectory = "D:\\";
                saveFileDialog1.DefaultExt = "bmp";
                saveFileDialog1.Filter = "All files (*.*)|*.*|bmp files (*.bmp)|*.bmp";
                saveFileDialog1.OverwritePrompt = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    ptbAnhdoi.Image.Save(saveFileDialog1.FileName);
            }
    
            private void btnThoat_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private static Bitmap Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize)
            {
                Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height);
    
                // make an exact copy of the bitmap provided
                using (Graphics graphics = System.Drawing.Graphics.FromImage(pixelated))
                    graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
                        new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
    
                // look at every pixel in the rectangle while making sure we're within the image bounds
                for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize)
                {
                    for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize)
                    {
                        Int32 offsetX = pixelateSize / 2;
                        Int32 offsetY = pixelateSize / 2;
    
                        // make sure that the offset is within the boundry of the image
                        while (xx + offsetX >= image.Width) offsetX--;
                        while (yy + offsetY >= image.Height) offsetY--;
    
                        // get the pixel color in the center of the soon to be pixelated area
                        Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY);
    
                        // for each pixel in the pixelate size, set it to the center color
                        for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++)
                            for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++)
                                pixelated.SetPixel(x, y, pixel);
                    }
                }
    
                return pixelated;
            }
    
            private void tbDiemanh_Scroll(object sender, EventArgs e)
            {
                biendoi = (int)tbDiemanh.Value;
                lblDiemanh.Text = biendoi.ToString();
            }
    
            private void btnBiendoi_Click(object sender, EventArgs e)
            {
                ptbAnhdoi.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + @"/temp/xuly.bmp");
                ptbAnhdoi.SizeMode = PictureBoxSizeMode.CenterImage;
    
                Bitmap bm = new Bitmap(ptbAnhdoi.Image);
                Bitmap bitmap = new Bitmap(bm);
                Pixelate(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), biendoi);
    
                ptbAnhdoi.Image = bitmap;
                ptbAnhdoi.SizeMode = PictureBoxSizeMode.CenterImage;
            }
        }
    }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    ui ui,.. giúp em với ạ ......................................

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Uầy, sáng mai trả lời, tối nay nghiên cứu cái đã.

    À nhầm, chiều mai mới trả lời được, nướng đến 12h trưa mà [IMG]images/smilies/smile.png[/IMG])

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    hix. Pro nhiệt tình với em quá, biết vậy là em vui rồi, chả phải tớ tối mai mà tới chiều ngày kia, ngày kìa cũng oke ạ [IMG]images/smilies/biggrin.png[/IMG]

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mình chưa xem kỹ project của bạn nhưng mình thấy hàm Pixelate chạy ổn, nhưng khi trượt scroll control bạn chưa xử lí độ trễ cho ảnh, mình đã test và thấy chạy bình thường.

    Mã:
    private void tbDiemanh_Scroll(object sender, EventArgs e){    biendoi = (int)tbDiemanh.Value;    lblDiemanh.Text = biendoi.ToString();    Image img = ptbAnhgoc.Image;    Bitmap temp = Pixelate(new Bitmap(img, img.Width, img.Height), new Rectangle(0,0, img.Width, img.Height), biendoi);    ptbAnhdoi.Image = temp;}

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi trinvh
    Mình chưa xem kỹ project của bạn nhưng mình thấy hàm Pixelate chạy ổn, nhưng khi trượt scroll control bạn chưa xử lí độ trễ cho ảnh, mình đã test và thấy chạy bình thường.

    Mã:
    private void tbDiemanh_Scroll(object sender, EventArgs e){    biendoi = (int)tbDiemanh.Value;    lblDiemanh.Text = biendoi.ToString();    Image img = ptbAnhgoc.Image;    Bitmap temp = Pixelate(new Bitmap(img, img.Width, img.Height), new Rectangle(0,0, img.Width, img.Height), biendoi);}
    Sửa như vầy là đúng rồi.
    Chú ý doạn này:

    Mã:
    Bitmap temp = Pixelate(new Bitmap(img, img.Width, img.Height), new Rectangle(0,0, img.Width, img.Height), biendoi);ptbAnhdoi.Image = temp;
    Tối qua mình cũng đã sửa và làm thêm tính năng dùng chuột để kéo chọn vùng muốn làm mờ.
    Mega: https://mega.co.nz/#!EpRWjL7K!Q5cwXH...EoiwXpAOUZoj0E
    Dropbox: https://www.dropbox.com/s4ll6ggj01...demo_boloc.zip
    Box: https://www.box.com/s/vxwt449xw2jc0q28wjas

    Bạn xem trong lớp tên XTest.

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Chân thành cảm ơn các cao thủ ạ, không biết bao giờ mới đạt trình độ như các anh.. hix, hix...

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •