C# Embed Image in New Outlook Mail Item from Standalone Application - c#

What is the best way of embedding an image into a new outlook mail item from a Stand Alone application. Not building an add-in for outlook.
Trying to embed or attach an existing picture to a new email item. I have read and looked at a lot of sources but most of these are tied into exchange or by using AddIn methods in Outlook.
User would see the image embedded into the new email and would just need to fill out the "To:" field. Would like to make the subject pre-populated in new email message as well from application.
Code Below: (I am trying to attach the picture that I capture below to an outlook email!
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;
using System.Net.Mail;
using System.Net.Mime;
namespace While_You_Were_Out
{
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
Show();
Rectangle l = Screen.PrimaryScreen.WorkingArea;
//Sets Position Manual all other Dialogs are set within parent center area.
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutDialog().ShowDialog(this);
}
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
Show();
Rectangle l = Screen.PrimaryScreen.WorkingArea;
//Sets Position Manual all other Dialogs are set within parent center area.
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}
private void trayRightClickMenu2_Opening(object sender, CancelEventArgs e)
{
}
private void toolStripMenuExit_Click(object sender, EventArgs e)
{
//Exit Application
Application.Exit();
}
private void toolStripMenuOpen1_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
Show();
Rectangle l = Screen.PrimaryScreen.WorkingArea;
//Sets Position Manual all other Dialogs are set within parent center area.
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}
private void button1_Click(object sender, EventArgs e)
{
//Hides to System Tray
this.Hide();
trayIcon.Visible = true;
//Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void sendNotificationToolStripMenuItem_Click(object sender, EventArgs e)
{
/* Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(#"C:\Razor\wywo_notification.jpg");
bmp.Dispose();*/
SaveAsBitmap(panel1, #"C:\Users\Razor\wywo_notification.jpg");
}
private void clearFormToolStripMenuItem_Click(object sender, EventArgs e)
{
txtBox6.Text = string.Empty;
txtBox8.Text = string.Empty;
txtBox9.Text = string.Empty;
}
public void SaveAsBitmap(Control control, string fileName)
{
//get the instance of the graphics from the control
Graphics g = control.CreateGraphics();
//new bitmap object to save the image
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
//Drawing control to the bitmap
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height));
bmp.Save(fileName);
bmp.Dispose();
}
private void sendToOutlook_Click(object sender, EventArgs e)
{
}
}
}
I was able to perform the function with VBS Script:
Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)
With olMsg
.To = "test#test.com"
'.CC = "cc#test.com"
'.BCC = "bcc#test.com"
.Subject = "Subject"
.HTMLBody = "<html><p>This is a picture.</p>" & _
"<img src='cid:wywo_notification.jpg'>"
'.Body = "<IMG align=baseline border=0 hspace=0 src=cid:myident>"
'.Attachments.Add "C:\users\doej\wywo_notification.jpg"
'.Attachments.Add "C:\users\doej\wywo_notification.jpg"
.Display
End With

I was able to solve my problem by relying on an VBS script called from within the application it was simple and solved my issue.

Create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.
Your HTML body would then need to reference that image attachment through the cid:
img src="cid:xyz"
where xyz is the value of the PR_ATTACH_CONTENT_ID property.
Look at an existing message with OutlookSpy (I am its author - click IMessage button).

Related

How to save an image in picture box after applying editing on it..These editing will show on picture when we open it in image viewer

i made a screen capturing application.. i applied drawing(simple lines) on it..
when i capture the screen and perform editing and save it..only the capture screen is save not the editing..i want to save all these editing on picture.. i m stuck here..
here is the code..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
bool paint = false;
SolidBrush color;
private MessageBoxButtons buttonType;
public Form1()
{
InitializeComponent();
}
private void picture_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, System.EventArgs e)
{
this.Opacity = 0;
time.Enabled = true;
}
private void button3_Click_1(object sender, System.EventArgs e)
{
SaveFileDialog savefiledialog1 = new SaveFileDialog();
try
{
savefiledialog1.Title = "Save File";
savefiledialog1.FileName = "Capture Screen";
savefiledialog1.Filter = "JPEG |*.jpeg";
if (savefiledialog1.ShowDialog() == DialogResult.OK)
picture.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
Application.Exit();
}
catch (Exception ex)
{
}
}
private void time_Tick_1(object sender, System.EventArgs e)
{
Rectangle bounds = default(Rectangle);
System.Drawing.Bitmap screenshot = default(System.Drawing.Bitmap);
Graphics graph = default(Graphics);
bounds = Screen.PrimaryScreen.Bounds;
screenshot = new System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
graph = Graphics.FromImage(screenshot);
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
picture.Image = screenshot;
time.Enabled = false;
this.Opacity = 100;
}
private void picture_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void picture_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void picture_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
Graphics g = picture.CreateGraphics();
color = new SolidBrush(Color.Blue);
g.FillEllipse(color, e.X, e.Y, 5, 5);
}
}
}
}

C# edit property of panel using run-time generated button

I'm a complete noob in C# programming. This is my problem: I have a form with a panel that is generated upon clicking button 1, the panel also containes a button (btnColor1) to change the backcolor of the panel. I would like to refer to the panel's backcolor in btnColor1, but I get the error "The name 'btnColor1' does not exist in the current context". How can I fix this?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEST_APP_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Panel myPanel1 = new Panel();
myPanel1.Location = new System.Drawing.Point(100, 50);
myPanel1.Name = "Panel 1";
myPanel1.Size = new System.Drawing.Size(200, 100);
myPanel1.BackColor = Color.Red;
TextBox textBox1 = new TextBox();
textBox1.Location = new Point(10, 50);
textBox1.Text = "empty field";
textBox1.Size = new Size(150, 30);
Button btnColor1 = new Button();
btnColor1.Location = new Point(10, 10);
btnColor1.Text = "GOLD";
btnColor1.Size = new Size(100, 30);
btnColor1.Click += myButton1_Click;
myPanel1.Controls.Add(textBox1);
myPanel1.Controls.Add(btnColor1);
Controls.Add(myPanel1);
}
private void myButton1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
btnColor1.BackColor = Color.Gold;
}
}
}
btnColor1 is declared in the scope of the button1_Click method so does not exist as a variable to reference in your event handler. Change the event handler to this:
private void myButton1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
(sender as Button).BackColor = Color.Gold;
}
You can try to use the parent property of the button to access the panel and the form:
private void myButton1_Click(object sender, EventArgs e)
{
Button but = (sender as Button);
but.BackColor = Color.Gold;
// this changes the color of the panel
but.Parent.BackColor = Color.Gold;
// this changes the color of the form containing the panel
if (but.Parent.Parent != null)
{
but.Parent.Parent.BackColor = Color.Gold;
}
}

AForge Convertation and problems with HD

First of all, I can't play and record HD video(1920x1080), when I change my camera resolution to 1920x1080 program show and record black background, if camera resolution is 720x576 it works without problems. I'm sure that the problem is in my solution because, when I use DesktopVideo(Software which is included with Blackmagic Decklink Studio 2) it shows HD video from camera.
Second, How do I convert video with AForge? like changing resolution and framerate, I can change codecs and bitrate, but when I change Resolution and FrameRate in "FileWriter.Open"
command I get Error Resolution and FrameRate must be the same as Bitmap I'm capturing from camera.
If anybody knows how to solve these problems please share information, thanks!
Here is the code
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 AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Video.VFW;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo = null;
private VideoCaptureDeviceForm captureDevice;
private Bitmap video;
//private AVIWriter AVIwriter = new AVIWriter();
private VideoFileWriter FileWriter = new VideoFileWriter();
private SaveFileDialog saveAvi;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
captureDevice = new VideoCaptureDeviceForm();
}
private void button1_Click(object sender, EventArgs e)
{
/////capture device list
if (captureDevice.ShowDialog(this) == DialogResult.OK)
{
VideoCaptureDevice videoSource = captureDevice.VideoDevice;
FinalVideo = captureDevice.VideoDevice;
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (butStop.Text == "Stop Record")
{
video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
//AVIwriter.Quality = 0;
FileWriter.WriteVideoFrame(video);
//AVIwriter.AddFrame(video);
}
else
{
video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
}
private void button2_Click(object sender, EventArgs e)
{
////record button
saveAvi = new SaveFileDialog();
saveAvi.Filter = "Avi Files (*.avi)|*.avi";
if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
FileWriter.Open(saveAvi.FileName, w, h,25,VideoCodec.Default,5000000);
FileWriter.WriteVideoFrame(video);
//AVIwriter.Open(saveAvi.FileName, w, h);
butStop.Text = "Stop Record";
//FinalVideo = captureDevice.VideoDevice;
//FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
//FinalVideo.Start();
}
}
private void butStop_Click(object sender, EventArgs e)
{
if (butStop.Text == "Stop Record")
{
butStop.Text = "Stop";
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
//this.FinalVideo.Stop();
FileWriter.Close();
//this.AVIwriter.Close();
pictureBox1.Image = null;
}
}
else
{
this.FinalVideo.Stop();
FileWriter.Close();
//this.AVIwriter.Close();
pictureBox1.Image = null;
}
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
this.FinalVideo.Stop();
FileWriter.Close();
//this.AVIwriter.Close();
}
}
}
}
The provided code simply captures and saves in the format currently active/selected for the capture device. In general, resolution change is an "expensive" operation is not available as a complimentary operation.
You want to change resolution right on capture device by switching it to more appropriate format, then capture in this resolution and have all further data manipulation in correct resolution. Available resolutions and options might be hardware specific there.
If/when no suitable capture resolution option is available, you typically resample the video to new resolution. Even though Windows API has suitable functionality, in your particular case you are interested to have this integrated with AForge library and you need to check its documentation to find out whether it provides a respective wrapper and whether it can be integrated into capture process overall (or, you need to write this code yourself to handle scaling).

Display an image into windows forms

I wanted to display an image to the windows forms, but i already did this and the image did not come out.
Where did I go wrong?
Here is the code:
private void Images(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("../SamuderaJayaMotor.png");
pb1.Location = new Point(100, 100);
pb1.Size = new Size(500, 500);
this.Controls.Add(pb1);
}
Here (http://www.dotnetperls.com/picturebox) there 3 ways to do this:
Like you are doing.
Using ImageLocation property of the PictureBox like:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "../SamuderaJayaMotor.png";
pb1.SizeMode = PictureBoxSizeMode.AutoSize;
}
Using an image from the web like:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "http://www.dotnetperls.com/favicon.ico";
pb1.SizeMode = PictureBoxSizeMode.AutoSize;
}
And please, be sure that "../SamuderaJayaMotor.png" is the correct path of the image that you are using.
There could be many reasons for this. A few that come up quickly to my mind:
Did you call this routine AFTER InitializeComponent()?
Is the path syntax you are using correct? Does it work if you try it in the debugger? Try using backslash (\) instead of Slash (/) and see.
This may be due to side-effects of some other code in your form. Try using the same code in a blank Form (with just the constructor and this function) and check.
I display images in windows forms when I put it in Load event like this:
private void Form1_Load( object sender , EventArgs e )
{
pictureBox1.ImageLocation = "./image.png"; //path to image
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Location = new Point(0, 0);
pb.Size = new Size(150, 150);
pb.Image = Image.FromFile("E:\\Wallpaper (204).jpg");
pb.Visible = true;
this.Controls.Add(pb);
}

Saving images using Aforge

i wrote a code for accessing webcam and on clicking, saving the picture to a folder.
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 AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
namespace cam
{
public partial class Form1 : Form
{
public static Bitmap _latestFrame;
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;
Bitmap bitmap;
private void Form1_Load(object sender, EventArgs e)
{
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in webcam)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bitmap;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = bitmap;
}
private void button3_Click(object sender, EventArgs e)
{
if (cam.IsRunning)
{
cam.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap current = (Bitmap)_latestFrame.Clone();
string ActiveDir = AppDomain.CurrentDomain.BaseDirectory;
string filepath = System.IO.Path.Combine(ActiveDir, #"D://picture/");
if (!System.IO.Directory.Exists(filepath))
{
System.IO.DirectoryInfo OutputDir = System.IO.Directory.CreateDirectory(filepath);
string fileName = System.IO.Path.Combine(filepath, #"name.bmp");
if (!System.IO.File.Exists(fileName))
{
current.Save(fileName);
}
}
current.Dispose();
}
}
}
In the button2 I have written the code for saving the picture, on building the program, an null reference exception is shown for the given line(Bitmap current = (Bitmap)_latestFrame.Clone();)
As far as I can see in your code, the new image frame is copied to your member variable bitmap. The static member _latestFrame never seem to be assigned.
Therefore, in your button2_Click method, change the first line to:
Bitmap current = (Bitmap)bitmap.Clone();
Now, provided you have received at least one frame from the webcam when you click the button, the frame should be properly saved.
I also think you are overworking the filepath setting in the button2_Click method. To begin with, simply verify that the picture can be properly saved to the active directory by changing your button2_Click method to this:
private void button2_Click(object sender, EventArgs e)
{
Bitmap current = (Bitmap)bitmap.Clone();
string filepath = Environment.CurrentDirectory;
string fileName = System.IO.Path.Combine(filepath, #"name.bmp");
current.Save(fileName);
current.Dispose();
}
This will ensure that a new image will be written to the "current directory" every time you click on the Capture button.
I have tested your code with the above changes, and it works flawlessly.

Categories