Adding multiple attachments to a mail, in c# windows forms - c#

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() {}")

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..?

Related

Error at sending a picture from server to client

I have 2 Apps (server - client ) .
The server is modified version of TVsharp (Application that stream local analog tv signal using RTLSDR)
Each frame of the streamed video is a grayscale array of bytes .
I have modified it so it re sizes and sends each frame for a client over TCP socket
The client is supposed to receive the frames through the socket as Image objects and displays them in a picture box
Im getting invalid parameters error .
After i added a delay (Thread.Sleep()) it started to display one frame and then it gives invalid parameter exception (after the sleeping time)
This is the part of TVsharp that dose the sending :
Grayscale is an array that contains the brightness for each pixel
private string drive = "E:\\";
private string file = "0";
private string extension = ".bmp";
private string path2 = "E:\\test\\";
private string fullpath;
private int file_counter = 0;
Bitmap bitmap2 = new Bitmap(_pictureWidth, _pictureHeight);
var data2 = bitmap2.LockBits(new Rectangle(Point.Empty, bitmap2.Size),
ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);
Marshal.Copy(GrayScaleValues, 0, data2.Scan0, GrayScaleValues.Length);
bitmap2.UnlockBits(data2);
bitmap2.Save(fullpath, System.Drawing.Imaging.ImageFormat.Bmp);
string Npath = path2 + file + extension;
Image img = Image.FromFile(fullpath);
// Size size = new Size(982, 543);
// ResizeImage(img, size);
Rectangle cropRect = new Rectangle(80, 0, 240, 175);
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(img, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
}
//double scale = 203 / 96;
int width = (int)(target.Width);
int height = (int)(target.Height);
BinaryWriter brn = new BinaryWriter(s);
System.Drawing.Bitmap bmpScaled = new System.Drawing.Bitmap(target, width, height);
bmpScaled.Save(Npath);
byte[] imageArray = File.ReadAllBytes(Npath);
sw.WriteLine(imageArray.Length.ToString());
sw.Flush();
// Thread.Sleep(500);
brn.Write(imageArray);
//Thread.Sleep(500);
_detectLevel = Convert.ToInt32(_maxSignalLevel * _detectorLevelCoef);
_agcSignalLevel = agcMaxLevel;
}
This client the client segment that suppose to get the frames and display them
flag = true;
TcpClient client = new TcpClient(textBox1.Text, Convert.ToInt32(textBox2.Text));
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.Flush();
BinaryFormatter formatter = new BinaryFormatter();
string msg = sr.ReadLine();
MessageBox.Show("It's working !! the message is " + msg);
BinaryReader Brn = new BinaryReader(s);
Thread.Sleep(5000);
while (flag)
{
// Thread.Sleep(5000);
int size = Convert.ToInt32(sr.ReadLine());
label3.Text = "Size is " + size;
byte[] imagerray = Brn.ReadBytes(size);
MemoryStream ms = new MemoryStream(imagerray);
Thread.Sleep(10000);
Image image = Image.FromStream(ms);
ResizeImage(image, this.Size);
pictureBox1.Image = image;
Thread.Sleep(10);
}
}

How record Audio from Kinect with NAudio

I would like to record audio from Kinect. I'm using NAudio library.
I found this code:
private void button2_Click(object sender, EventArgs e) //Play Button
{
fileName = "lastReplay.wav";
FileStream FS_Write = File.OpenWrite("lastReplay.wav");
FS_Write.Close();
int deviceNumber = sourceList.SelectedItems[0].Index;
sourceStream = new NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100,
NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
NAudio.Wave.WaveInProvider waveIn = new
NAudio.Wave.WaveInProvider(sourceStream);
sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>
(sourceStream_DataAvailable);
waveWriter = new NAudio.Wave.WaveFileWriter(fileName, sourceStream.WaveFormat);
sourceStream.StartRecording();
}
private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
{
if (waveWriter == null) return;
waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
waveWriter.Flush();
}
When i start the kinect the audio channel need to be started, so I use this:
Stream audioStream = this.sensor.AudioSource.Start();
How can I change the button2_click method for accepting audioStream?
I have solved my problem, the code is this:
static void WriteWavHeader(Stream stream, int dataLength)
{
//We need to use a memory stream because the BinaryWriter will close the underlying stream when it is closed
using (var memStream = new MemoryStream(64))
{
int cbFormat = 18; //sizeof(WAVEFORMATEX)
WAVEFORMATEX format = new WAVEFORMATEX()
{
wFormatTag = 1,
nChannels = 1,
nSamplesPerSec = 16000,
nAvgBytesPerSec = 32000,
nBlockAlign = 2,
wBitsPerSample = 16,
cbSize = 0
};
using (var bw = new BinaryWriter(memStream))
{
//RIFF header
WriteString(memStream, "RIFF");
bw.Write(dataLength + cbFormat + 4); //File size - 8
WriteString(memStream, "WAVE");
WriteString(memStream, "fmt ");
bw.Write(cbFormat);
//WAVEFORMATEX
bw.Write(format.wFormatTag);
bw.Write(format.nChannels);
bw.Write(format.nSamplesPerSec);
bw.Write(format.nAvgBytesPerSec);
bw.Write(format.nBlockAlign);
bw.Write(format.wBitsPerSample);
bw.Write(format.cbSize);
//data header
WriteString(memStream, "data");
//bw.Write(dataLength);
bw.Write(dataLength);
memStream.WriteTo(stream);
}
}
}
But with this method i should know before the time of file audio. I put dataLength parameter. If i use this code it found but now i would like to use this method never dataLength parameter because i don't know before the duration of file Audio, how can I modify it?

ASP.NET uploading a file to Amazon S3

I am in the process of uploading images to Amazon S3, however i keep getting the error "Please specify either a Filename, provide a FileStream or provide a ContentBody to PUT an object into S3."
Basically i am uploading an image from a fileupload control and then hitting the code below. It uploads locally fine, but not to Amazon. The Credentials are alright so it only errors when it comes to uplaoding.
Can anyone see why this is happening please?
protected void uploadImg(int prodId, int prodFormat)
{
if (imgPack.HasFile)
{
string fileExt = Path.GetExtension(imgPack.PostedFile.FileName);
string filename = "img" + prodId + ".jpg";
// Specify the upload directory
string directory = Server.MapPath(#"\images\packshots\");
if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".png")
{
if (packUK.PostedFile.ContentLength < 716800)
{
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(packUK.FileContent);
// Calculate the new image dimensions
decimal origWidth = originalBMP.Width;
decimal origHeight = originalBMP.Height;
decimal sngRatio = origHeight / origWidth;
int newHeight = 354; //hight in pixels
decimal newWidth_temp = newHeight / sngRatio;
int newWidth = Convert.ToInt16(newWidth_temp);
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
string accessKey = "KEY HERE";
string secretKey = "KEY HERE";
AmazonS3 client;
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
PutObjectRequest request = new PutObjectRequest();
request.BucketName="MyBucket";
request.CannedACL = S3CannedACL.PublicRead;
request.Key = "images/" + filename;
S3Response response = client.PutObject(request);
}
//newBMP.Save(directory + filename);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
}
}
else
{
notifybar.Attributes.Add("style", "display:block;");
notifybar.Attributes.Add("class", "failed");
notifyText.Text = "Error Text Here";
}
}
else
{
notifybar.Attributes.Add("style", "display:block;");
notifybar.Attributes.Add("class", "failed");
notifyText.Text = "Error Text Here";
}
}
You need to assign File or InputStream property of PutObjectRequest object. The code fragment should look like this one:
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
var stream = new System.IO.MemoryStream();
originalBMP.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
PutObjectRequest request = new PutObjectRequest();
request.InputStream = stream;
request.BucketName="MyBucket";
request.CannedACL = S3CannedACL.PublicRead;
request.Key = "images/" + filename;
S3Response response = client.PutObject(request);
}

Retrieving favicon as icon instead of image

I used the favicon-code I found here to retrieve the favicon of the site loaded in the browser element.
I want to use this favicon as the icon of my Windows Form.
Thanks to JP Hellemons this code works:
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
this.Icon = favicon(GetActiveBrowser().Url);
}
private WebBrowser GetActiveBrowser() {
return (WebBrowser)tabs.SelectedTab.Controls[0];
}
private Icon favicon(Uri url) {
WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url.Host + "/favicon.ico");
Bitmap bm = new Bitmap(32, 32);
MemoryStream memStream;
using (Stream response = request.GetResponse().GetResponseStream()) {
memStream = new MemoryStream();
byte[] buffer = new byte[1024];
int byteCount;
do {
byteCount = response.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, byteCount);
} while (byteCount > 0);
}
bm = new Bitmap(Image.FromStream(memStream));
if (bm != null) {
Icon ic = Icon.FromHandle(bm.GetHicon());
return ic;
} else
return Properties.Resources.GZbrowser;
}
According to this documentation, it should work from stream.
http://msdn.microsoft.com/en-us/library/system.drawing.icon.aspx
I used this article: http://odetocode.com/Blogs/scott/archive/2004/10/05/webrequest-and-binary-data.aspx
WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url.Host + "/favicon.ico");
Icon ic = new Icon(); // put default here
Bitmap bm = new Bitmap();
try
{
using(WebResponse response = request.GetResponse())
{
using(Stream responseStream = response.GetResponseStream())
{
using(MemoryStream ms = new MemoryStream())
{
var tmp = Image.FromStream(ms); // changed bitmap to image
bm = new Bitmap(tmp);
}
}
}
}catch{}
if(bm != null)
{
ic = Icon.FromHandle(bm.GetHicon);
}
return ic;
Edit: something like this should do it
Edit2: changed some things in the answer. Can you try this?
Final edit: (lol)
Just tested this in a windows form app and this works! :)
Uri url = new Uri("http://www.google.nl");
WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url.Host + "/favicon.ico");
Bitmap bm = new Bitmap(32,32);
MemoryStream memStream;
using (Stream response = request.GetResponse().GetResponseStream())
{
memStream = new MemoryStream();
byte[] buffer = new byte[1024];
int byteCount;
do
{
byteCount = response.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, byteCount);
} while (byteCount > 0);
}
bm = new Bitmap(Image.FromStream(memStream));
if (bm != null)
{
Icon ic = Icon.FromHandle(bm.GetHicon());
this.Icon = ic;
}
Read response byte array first, than create MemoryStream of it and create icon from that MemoryStream.
Network stream do not support seek operations that seem to be necessary for creating an icon.

How to convert a XPS file to an image in high quality (rather than blurry low resolution)?

I'm trying to convert an XPS with WPF.
The idea is that these images can be loaded with silverlight 4, for this I am using the following code:
// XPS Document
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
// The number of pages
PageCount = docSeq.References[0].GetDocument(false).Pages.Count;
DocumentPage sizePage = docSeq.DocumentPaginator.GetPage(0);
PageHeight = sizePage.Size.Height;
PageWidth = sizePage.Size.Width;
// Scale dimensions from 96 dpi to 600 dpi.
double scale = 300/ 96;
// Convert a XPS page to a PNG file
for (int pageNum = 0; pageNum < PageCount; pageNum++)
{
DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
BitmapImage bitmap = new BitmapImage();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)(scale * (docPage.Size.Height + 1)),
(int)(scale * (docPage.Size.Height + 1)),
scale * 96,
scale * 96, PixelFormats.Pbgra32);
renderTarget.Render(docPage.Visual);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
FileStream pageOutStream = new FileStream(name + ".Page" + pageNum + ".png", FileMode.Create, FileAccess.Write);
encoder.Save(pageOutStream);
pageOutStream.Close();
This code is taken from http://xpsreader.codeplex.com/ a project to convert an XPS document.
works great!
But the problem is that the image is low resolution and blurry.
I researched and found that RenderTargetBitmap and find on this page:
http://www.codeproject.com/Questions/213737/Render-target-bitmap-quality-issues
The issue here is you Have That does not use hardware RenderTargetBitmap rendering.
One solution is to use DirectX with WPF to do this, but have not found any clear example to show me the right way to do it.
I appreciate suggestions. Thanks in advance.
Update:I attached the XPS document, I am trying to convert the image
Please download test.xps
There is a project named xps2img on sourceforge.net which converts an xps to image. It is made in C# and also contains the source code. Check it out. It will help you to achieve what you want.
http://sourceforge.net/projects/xps2img/files/
I saw in this post and in many others that peoples have problems with conversion of DocumentPage to Image and saving it on HDD.
This method took all pages from document viewer and save them on HDD as jpg images.
public void SaveDocumentPagesToImages(IDocumentPaginatorSource document, string dirPath)
{
if (string.IsNullOrEmpty(dirPath)) return;
if (dirPath[dirPath.Length - 1] != '\\')
dirPath += "\\";
if (!Directory.Exists(dirPath)) return;
MemoryStream[] streams = null;
try
{
int pageCount = document.DocumentPaginator.PageCount;
DocumentPage[] pages = new DocumentPage[pageCount];
for (int i = 0; i < pageCount; i++)
pages[i] = document.DocumentPaginator.GetPage(i);
streams = new MemoryStream[pages.Count()];
for (int i = 0; i < pages.Count(); i++)
{
DocumentPage source = pages[i];
streams[i] = new MemoryStream();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)source.Size.Width,
(int)source.Size.Height,
96, // WPF (Avalon) units are 96dpi based
96,
System.Windows.Media.PixelFormats.Default);
renderTarget.Render(source.Visual);
JpegBitmapEncoder encoder = new JpegBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.QualityLevel = 100;
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
encoder.Save(streams[i]);
FileStream file = new FileStream(dirPath + "Page_" + (i+1) + ".jpg", FileMode.CreateNew);
file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length);
file.Close();
streams[i].Position = 0;
}
}
catch (Exception e1)
{
throw e1;
}
finally
{
if (streams != null)
{
foreach (MemoryStream stream in streams)
{
stream.Close();
stream.Dispose();
}
}
}
}
A nuget package based on xps2img is now available:
https://www.nuget.org/packages/xps2img/
Api available here:
https://github.com/peters/xps2img#usage
private IList<byte[]> GetTifPagesFromXps(string xXpsFileName, double xQuality)
{
using (var xpsDoc = new XpsDocument(xXpsFileName, FileAccess.Read))
{
var docSeq = xpsDoc.GetFixedDocumentSequence();
var tifPages = new List<byte[]>();
for (var i = 0; i < docSeq.DocumentPaginator.PageCount; i++)
{
using (var docPage = docSeq.DocumentPaginator.GetPage(i))
{
var renderTarget = new RenderTargetBitmap((int)(docPage.Size.Width * xQuality), (int)(docPage.Size.Height * xQuality), 96 * xQuality, 96 * xQuality, PixelFormats.Default);
renderTarget.Render(docPage.Visual);
var jpegEncoder = new JpegBitmapEncoder { QualityLevel = 100 };
jpegEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
byte[] buffer;
using (var memoryStream = new MemoryStream())
{
jpegEncoder.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
buffer = memoryStream.GetBuffer();
}
tifPages.Add(buffer);
}
}
xpsDoc.Close();
return tifPages.ToArray();
}
}

Categories