Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm making an c# and Windows Forms (Classic Windows app like notepad, paint etc.) app that has a feature to get screenshot and send it via mail. However, It can only take 6 pictures now (I can add more, but I don't want to add more code, I want to make it programmatically), how can I make it send more or less, as set by user, outside of app?

Timer1 sends mail. Timer2 takes screenshot. resimoran is an int which is image ratio of resizing, it's 1 by default. counter is an int, It's working right now...

here is my code:

    private Bitmap Screenshot()
    {
        Bitmap Screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics GFX = Graphics.FromImage(Screenshot);
        GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
        return Screenshot;
    }

    void SendReport()
    {
        MailMessage mail;
        var fromAddress = new MailAddress(frommail, fromname);
        var toAddress = new MailAddress(alici, aliciname);
        string fromPassword = mailpass;

        var smtp = new SmtpClient
        {
            Host = mailhostaddress,
            Port = mailport,
            EnableSsl = sslenabled,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = usedefaultcre,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (mail = new MailMessage(fromAddress, toAddress)
        {
            Subject = konu + DateTime.Now,
            Body = "None of your businness!"
        })
        {
            mail.Attachments.Add(attach1);
            mail.Attachments.Add(attach2);
            mail.Attachments.Add(attach3);
            mail.Attachments.Add(attach4);
            mail.Attachments.Add(attach5);
            mail.Attachments.Add(attach6);

                    smtp.Send(mail);
        }
    }

private void timer1_Tick(object sender, EventArgs e)
    {
        SendReport();
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        counter++;
        if (counter == 1)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen1.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach1 = new Attachment(streamer, "screen1.jpg");
        }
        else if (counter == 2)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen2.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach2 = new Attachment(streamer, "screen2.jpg");
        }
        else if (counter == 3)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen3.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach3 = new Attachment(streamer, "screen3.jpg");
        }
        else if (counter == 4)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen4.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach4 = new Attachment(streamer, "screen4.jpg");
        }
        else if (counter == 5)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen5.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach5 = new Attachment(streamer, "screen5.jpg");
        }
        else if (counter == 6)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen6.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach6 = new Attachment(streamer, "screen6.jpg");
            counter = 0;
        }
    }

public static Bitmap resizeImage(Bitmap imgToResize, Size size)
        {
            return (new Bitmap(imgToResize, size));
        }

And, please, give me answers in C#, not in English! (not "do this: MSDN bla bla", but "try this void lolnocodezhere() {}")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
231 views
Welcome To Ask or Share your Answers For Others

1 Answer

List<T> is your friend.

you declare it at a proper place as

List<Attachment> attachments = new List<Attachment>();

Then you replace your 6 blocks with a single one where you do a

attachments.Add(new Attachment(streamer, "screen.jpg");)

and when the right time has come you do a

foreach(Attachment a in attachments ) mail.Attachments.Add(a);

After successfully sending the mail you delete the collection like this:

attachments.Clear();

It is up to you to control things like counters, the screen images etc.

Btw: mail.Attachmentsis just such a collection and maybe you want to use it directly..?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...