Saving a file in a WCF service to Images folder - c#

So here is the problem. i am saving images in binary form to db. In the WCF service i created I want to save the file to a folder within the project i.e (/Images).
I normally use this code
//picbin is binary image data fetched from db
ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(picbin);
img.Save(HttpContext.Current.Server.MapPath(imagePath + picture_id + ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);// imagePath ="Images/"
The problem is HttpContext.Current.Server.MapPath does not work in WCF services. so how can I get the physical path to save the image. Also what Url can i Get to access the image
I tried Using HostingEnvironment.MapPath, but the path it returns is always null.

You can use HostingEnvironment.MapPath.
please check below link
https://stackoverflow.com/a/10384987/2699211

img.Save("website absolute path/" +(imagePath + picture_id + ".Jpeg"),
you need to provide the absolute path. it works on my side.

Related

Converting aspx page to image file

I have c# dynamic aspx page after new property add I create for record brochure
http://veneristurkey.com/admin/Brochure.aspx?Admin=PropertiesBrochureA4&id=36
but I want to this convert image file I am searching on internet but all with webbrowser and windows forms. I need on page load show not css type also image file. jpg, png or tiff how i can do this. i need to see sample code..
saving aspx page into an image 2
As I mentioned in my comment, your best bet is to opt for attempting to render HTML to an image.
Here is the link for a library that will allow your to render html to an image:
http://htmlrenderer.codeplex.com/
Here is code that does exactly what you're asking:
http://amoghnatu.wordpress.com/2013/05/13/converting-html-text-to-image-using-c/
Now all you have left is to get the html, since I'm assuming you don't want this to render to the browser prior to generating this image - you should look into grabbing the rendered html from the aspx page on the server prior to returning it, and then just return the image. To render a page:
https://stackoverflow.com/a/647866/1017882
Sorted.
If you do not mind using a commandline tool you can have a look at wkhtmltopdf. The package include a wkhtmltoimage component that can be used to convert HTML to image, using
wkhtmltoimage [URL] [Image Path]
Codaxy also wrote a wkhtmltopdf c# wrapper available through the NuGet package manager. I'm not sure if the wkhtmltoimage component was included, but it should be easy enough to figure out how they wrap the wkhtml components.
i fixed my problem with screenshot machine API they are my code..
public void resimyap()
{
var procad = WS.Satiliklars.Where(v => v.ForSaleID == int.Parse(Request.QueryString["id"])).FirstOrDefault();
var imageBytes = GetBytesFromUrl("http://api.screenshotmachine.com/?key=xxxxxx&size=F&url=http://xxxxxxx.com/a4.aspx?id=" + procad.ForSaleID);
string root = Server.MapPath("~/");
// clean up the path
if (!root.EndsWith(#"\"))
root += #"\";
// make a folder to store the images in
string fileDirectory = root + #"\images\a4en\";
// create the folder if it does not exist
if (!System.IO.Directory.Exists(fileDirectory))
System.IO.Directory.CreateDirectory(fileDirectory);
WriteBytesToFile( fileDirectory + + procad.ForSaleID + ".png", imageBytes);
Yes i also try wkhtmltopdf c# wrapper but in pdf or image converting time my computer fan goin crayz. also i must upload server exe file and my hosting firm didnt support them

FiIeInfo.CreationTime. How to read correct value?

I have file upload control on .aspx page, I want to upload an image at a time whose location is not always the same, could be in same directory as .sln, on desktop, anywhere.
I am using this code to read the file creation time
string savePath = MapPath("~/" + Path.GetFileName(e.FileName));
FileInfo MyFileInfo = new FileInfo(savePath);
string dt = MyFileInfo.CreationTime.Day.ToString();
string mn = MyFileInfo.CreationTime.Month.ToString();
string yr = MyFileInfo.CreationTime.Year.ToString();
I have noticed one thing that
If the image is in the same folder as the website, it gives me correct values for all
dt , mn and yr
But if it's outside that location, it always gives me same value everytime
{01/01/1601 00:00:00}
Not sure how to sort this out?
any advice, helpful code? thanks
It seems you do not understand the client/server barrier yet. Paths from the client are not meaningful on the server.
It so happens that your website is running on the same machine as the client because you are debugging locally. That is just a coincidence and it allows your code to sometimes work (by coincidence).
Proper file uploading works by ignoring the path and reading from the stream provided by the file upload control. A file upload is just a stream of bytes to the server.

C# - Get Image path from Client Machine

I want to get the path of the Image which is saved in Client Machine. I know the Path and the file name of the Image. By using FileUpload i can do it, but without using fileupload is it possible to get the path of the file ??.
My Scenario is given below,
Public void ConverttoByte()
{
//Get the image path from web.config & this image is in client machine
string strConfig = #"C:\Manikandan\image\image1.jpg";
MemoryStream MS = new MemoryStream();
Byte[] data;
int fiFileSize;
System.Drawing.Image image;
image = System.Drawing.Image.FromFile(strConfig);
image.Save(MS, System.Drawing.Imaging.ImageFormat.Gif);
data = MS.ToArray();
CallDBMethod(data);
}
Here I converted the image as Byte and I called the CallDBMethod to insert this byte details to DB..
This image is available in client machine, but not in server machine..
So, how to i get this image path from client machine & how can i solve this?
Without the File upload control, It is not possible unless you create an Activex control (may not work in all browsers. User has to give permission to run this control).
In a web application, You can not take ( steal) any file from the user's computer without them making an action to do so( ex : selecting the file in the File Input control and clicking some upload button).
If you want the full file path of the file user selected in the file upload control, you can get by HttpPostedFile.FileName property which gives you the fully qualified name of the file on the client (Ex : C:\MySomeFolder\SomeFile.jpg).
string fullPath=FileUpload1.HttpPostedFile.FileName
Assuming FileUpload1 is the ID of the File Upload control.
According to my knowledge it is not possible. You can't get the client's image path. The browser does not allow us to get the path. Using fileupload control you can also not get the complete path.
A similar question by me
How to Get complete file path using file upload control in asp.net or any other way?

Accessing image from other server

I have image file placed on the one server and application on other server.
I want to access that image, below code I have written:
On default.aspx, I have
<asp:Image ID="Image1" runat="server" ImageUrl= "GetImage.aspx?imgName=MyImage.jpg" />
and on GetImage.aspx, I have written the below code on page_load
protected void Page_Load(object sender, EventArgs e)
{
// Changing the page's content type to indicate the page is returning an image
Response.ContentType = "image/jpg";
var imageName = Request.QueryString["imgName"];
var path = "//SERVER/FOLDER/" + imageName;
if ((string.IsNullOrEmpty(imageName) == false))
{
// Retrieving the image
System.Drawing.Image fullSizeImg;
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
// Writing the image directly to the output stream
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
// Cleaning up the image
fullSizeImg.Dispose();
}
}
But I am getting error at
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
Please let me know where I am incorrect. Do I need to anything else other than Server.MapPath ? because my image is on other server.
EDIT
I have image folder in my computer
I have created a web app in other computer [same network], deployed
in IIS, image is displayed correctly. With path like
http://10.67.XX.XX/websiteName/Default.aspx
but when I am trying to access the same from my comupter or any other
computer, I am not able to see the image.
You shouldn't use Server.MapPath. This is used to map virtual paths under your site to physical paths under file system. If the file exists on another server, just access it by name directly, without Server.MapPath.
The answer above is incorrect because the images reside on a separate server.
So System.Images will not no where the image is.
Secondly you have to use server.Mappath for system images, it requires a windows path i.e. c:\blah\blah\whatever.jpg.
you will need to use \\server\C$\folder\image.jpg this works well with system.image.FromFile
Also saving you can also utilize this.
Cheers

Image to HttpPostedFileBase - or a way to store an Image file in server filesystem

I have a WCF RESTful service that has the purpose of receiving a base64-encoded string that represents an image and store it in the server filesystem, in a manner similar to how a HttpPostedFile does it with the following code (file is a HttpPostedFileBase variable):
file.SaveAs(Server.MapPath("..\\Content\\UploadedImages\\") + id + file.FileName.Replace(" ",""));
So far, I can create an System.Drawing.Image object from the base64 string without any problems. However, I can't seem to create a HttpPostedFileBase variable that can take this image and invoke the method put in the code brackets?
Is there any way I can do it? According to this page, I can't, so the real question would be:
How can I save an System.Drawing.Image file in the filesystem of my server, so I can just keep a reference to the file in the database? Is it even possible?
Thanks in advance for your time! :D
Assuming you have an Image instance you could save it like this on the server inside the UploadedImages folder:
using (Image image = ...)
{
var imagesFolder = Server.MapPath("~/Content/UploadedImages");
var filename = Path.Combine(imagesFolder, id, file.FileName.Replace(" ",""));
image.Save(filename);
}

Categories