I am creating an add in for outlook 2007, that embeds smileys to the body of an email.
And the method i am using is as follows:
if (!string.IsNullOrEmpty(mail.HTMLBody) && mail.HTMLBody.ToLower().Contains("</body>"))
{
int mailBodyLength;
if (mail.Body == null)
{
mailBodyLength = 0;
}
else
{
mailBodyLength = mail.Body.Length;
}
//Get Image + Link
Image imagePath = image;
object linkAddress = "http://www.pentavida.cl";
//CONTENT-ID
const string SchemaPR_ATTACH_CONTENT_ID = #"http://schemas.microsoft.com/mapi/proptag/0x3712001E";
string contentID = Guid.NewGuid().ToString();
//Attach image
mail.Attachments.Add(imagePath, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, mailBodyLength, Type.Missing);
mail.Attachments[mail.Attachments.Count].PropertyAccessor.SetProperties(SchemaPR_ATTACH_CONTENT_ID, contentID);
//Create and add banner
string banner = string.Format(#"<br/><a href=""{0}"" ><img src=""cid:{1}"" ></a></body>", linkAddress, contentID);
mail.HTMLBody = mail.HTMLBody.Replace("</body>", banner);
mail.Save();
}
In this line : mail.Attachments.Add(imagePath, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, mailBodyLength, Type.Missing);
I am adding an image object (imagePath) but its throwing an exception, it worked when i put a full path of an image in the method, now i am passing through an image that was got from my resources folder. I am assuming that the method is failing because it needs a path of the image and not the Image object. How do i get the Image path name from this Image object to pass into this method?
I have an image that i got from my resources folder, i need the path of that image. How do i get that path?
Image imagePath = image;
I need the image path because i am adding the image to a new mail in outlook.
But when i pass through the Image object it throws an exception of:
Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))
But passing a string works fine.
thanks in advance.
Related
I'm Trying to save the image after cropping into folder &image path to the database using croppie but it convert to base64 & i also dont know how to send this data to controller & how can i save the image into a folder & its path to the database.
I already try to save file.write function but it was not sending back to image data to the controller
public ActionResult AddProduct(Tbl_Product product,HttpPostedFileBase file_photo)
{
string name = null;
string ext = null;
if (ModelState.IsValid==true)
{
if (file_photo != null)
{
name = Path.GetFileNameWithoutExtension(file_photo.FileName);
ext = Path.GetExtension(file_photo.FileName);
string path = Path.Combine(Server.MapPath("~/ProductImages"), name + ext);
file_photo.SaveAs(path);
}
product.ProductImage = name + ext;
product.CreatedDate = DateTime.Now;
_unitofwork.GetRepositoryInstance<Tbl_Product>().Add(product);
return RedirectToAction("Product");
}
else
{
ViewBag.CategoryList = GetCategory();
return View();
}
}
i want to save image in folder & path to database but it shows base64 image
I have written a complete post on using croppie.js with c#
A function to set croppie,
Function to crop the Image,
Then send the image date to the web method by ajax,
Convert image data to image and save it in the folder.
Here is the link,
https://shaktisinghcheema.com/image-upload-with-cropping/
Also, you may run into an issue of timeout when sending image data to web method,
Here is the link of solution to it:
Error while sending base64 string through json
I hope this might help someone who lands on this question.
In croppie, after cropping the image you will get result in base64 format, keep this base64 in some hidden field like below,
$('#imagebase64').val(base64_data);
And in you controller action method, use the following code to store image in folder.
if (product.imagebase64 != null)
{
try
{
var base64Data = Regex.Match(product.imagebase64, #"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
byte[] imageBytes = Convert.FromBase64String(base64Data);
string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss"); // You can write custom name here
string path = Path.Combine(Server.MapPath("~/ProductImages"), filename + ".jpg");
System.IO.File.WriteAllBytes(path, imageBytes);
}
catch (Exception ex)
{
}
}
Currently my code can able to do capture image and saved in defined location but if i try the same in second time image is overwrite so if same file name present in that folder, we have to change name of file dynamically.
How can I do that ?
Present screen capturing code is:
private void CaptureMyScreen()
{
try
{
//Creating a new Bitmap object
Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
//Creating a Rectangle object which will capture our Current Screen
Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
//Creating a New Graphics Object
Graphics captureGraphics = Graphics.FromImage(captureBitmap);
//Copying Image from The Screen
captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size);
//Saving the Image File (I am here Saving it in My D drive).
captureBitmap.Save(#"D:\Capture.jpg", ImageFormat.Jpeg);
//Displaying the Successfull Result
MessageBox.Show("Screen Captured");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
You can use GUID to get unique name for every capture file. Something like
string guid = Guid.NewGuid().ToString();
captureBitmap.Save(#"D:\Capture-" + guid + ".jpg",ImageFormat.Jpeg);
or, use current date and time for that, like this:
string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
captureBitmap.Save(#"D:\Capture-" + timestamp + ".jpg",ImageFormat.Jpeg);
I am trying to create a file path, at the end of which will be a "images" folder that the program will use with the Image command to load jpeg files.
I want the program to dynamically know to load the jpeg files from the image folder where ever the executable is launched.
I was able to find this on this site, I added the 2 bottom code lines:
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return System.IO.Path.GetDirectoryName(path);
}
}
public static string imagePath = #"\images";
public static string finalImagePath = AssemblyDirectory + imagePath;
If I set a break point, the 'finalImagePath' is:
"C:\Users\My Name\Documents\Visual Studio 2010\Projects\Universal Serial Diagnostics\bin\Debug\images"
Which is correct, but how do I incorporate that with:
Image image = Image.FromFile(#"C:\Users\My Name\Desktop\Dip\ENV500008.jpg");
Replacing the hard coded path with the dynamic path.
The ENV500008.jpg would be stored in the images folder.
Thank you.
string path = AppDomain.CurrentDomain.BaseDirectory + "ENV500008.jpg";
if (System.IO.File.Exists(path))
{
Image image = Image.FromFile(path);
// .. the rest of the code that uses the image ..
}
Image image = Image.FromFile(#finalImagePath + "\\ENV500008.jpg");
Image image5 = Image.FromFile(#finalImagePath + "\\ENV510829-2.jpg");
public static string GetAnyPath(string fileName)
{
//my path where i want my file to be created is : "C:\\Users\\{my-system-name}\\Desktop\\Me\\create-file\\CreateFile\\CreateFile\\FilesPosition\\firstjson.json"
var basePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath.Split(new string[] { "\\CreateFile" }, StringSplitOptions.None)[0];
var filePath = Path.Combine(basePath, $"CreateFile\\CreateFile\\FilesPosition\\{fileName}.json");
return filePath;
}
change .json to any type according to need
refer :https://github.com/swinalkm/create-file/tree/main/CreateFile/CreateFile
I have a field in my app (c#) to save an image into the database. I written the following code to save the image into a folder and then save the path into the database. But the image is not getting saved into the folder.
string imgName = FileUpload1.FileName.ToString();
string imgPath = null;
if (imgName == "")
{
//int taxiid = Convert.ToInt32(HiddenField1.Value);
Taxi t = null;
t = Taxi.Owner_GetByID(tx.Taxi_Id, USM.OrgId);
imgPath = t.CarImage;
}
else
{
imgPath = "ImageStorage/" + imgName;
}
FileUpload1.SaveAs(Server.MapPath(imgPath));
tx.CarImage = imgPath;
I think your problem is that you add the name to the Path I try it and for me it works fine if I save it like this:
FileUpload1.SaveAs(Server.MapPath("ImageStorage") + imgName);
And as #Rahul mentioned add a try catch to prevent errors.
And you check
if (imgName == "")
According to my understanding it's not posible that imgName is "" but anyway you better add a check if the fileupload has a file.
if (FileUploadControl.HasFile)
filename = Path.GetFileName(FileUpload.FileName);
HttpPostedFile pf = FileUpload.PostedFile;
System.Drawing.Image img2 = System.Drawing.Image.FromStream(pf.InputStream);
System.Drawing.Image bmp2 = img2.GetThumbnailImage(200, 210, null, IntPtr.Zero);
Imagename = objUser.UserID + filename;
Imagepath = "D:\\Shopy_Web_21-6-12\\Shopy\\Images" + Imagename;
bmp2.Save(Path.Combine(#"D:\Shopy_Web_21-6-12\Shopy\Images", Imagename));
I've converted the file upload in to two thumbnails and saved them locally, but now I need retrieve the image to display it on the user's profile. How can I get the image to display from where I've stored it?
You can create a virtual path in your application say: ..Images/ and save your images in that folder.
and after that use the below one to fetch the image URL:
string str = Server.MapPath("Images/" + Filename);
Now you will get the url to your image that can be displayed directly.
Link your images folder (...\Shopy\Images) to a virtual folder under your app so then you can link to them within <img> elements.