What I am trying to do is, I want to save an image ("scanned image of receipt") upon each ticket generation and store its reference(image name) in mssql database.
Scenario:
The user will select the image from a source through OpenDialogBox & will click save button. Now what I want my application to do is, copy the file from source, change its file name to TicketID (TicketID will be unique everytime so the image name will always remain unique) and then save to a specific folder (which will store all images) and store the filename in database.
I have never used images in C# before, so I have no idea on how to actually do it. So I would really appreciate if someone could link me to a tutorial or something...
P.S. I am using visual studio 2012 and MQ SQL Server 2012.
you can do the following
1- on the save event assuming that your scanned Image called image
var ticketID=Guid.NewGuid().ToString(); // this method will ensure that the name of image will be unique all the time
var path="savingPath" + "/" + ticketID + ".jpg";
// the image path you can save it in the database
image.Save(path);
2- when you want to load the image
// retrieve the image path from database
var image=Image.FromFile(path);
hope that this will help you
If you don't need anything overly fancy in terms of processing the System.Drawing library should meet your needs.
Depending on how you want to architect your app you can pass a directory path directly into the constructor of your Image implementation. e.g.
var myImage = new Bitmap("C:\\somepath\\filename.jpg");
Or you could abstract away the location access you can just pass in a stream (which comes from a byte array, file operation, web request, whatever)
var myImage = new Bitmap(stream);
Saving an image is easy. The image class has a save method
image.Save("C:\\somepath\\filename2.jpg")
http://msdn.microsoft.com/en-us/library/9t4syfhh(v=vs.110).aspx
Related
I'm writing a WinForms application. I created a Google Doc template file that contains placeholders like {{name}} for various text elements. I can successfully make a copy of this document and use the BatchUpdateDocumentRequest to modify them just fine.
However, I also have an embedded image in the document. I can obtain the objectId for this image just fine. I either want to replace this image with another or remove it from my template and then append my new image to the end of the document. In both cases, the InsertInlineImage or ReplaceImage classes require a URI of the image to insert or replace with. This is where I have an issue.
The image itself has been captured from a control on the WinForms. Its actually a chart. I've saved the image in PNG format since I know that is one of the formats supported by Google drive/docs. I figured in order to use it in the batch update, I would need to upload it first, so I did and got its file id and webcontentlink back in the response.
I'm not locked into any particular way of doing this. I originally tried creating an HTML file, uploading but then it would strip the image from it, so became useless, so I switched gears to using a Google Doc as my template and just try to replace elements in it instead. This went well until I got to the image.
Essentially no matter what I try to specify as the URI, it says the file in not in a supported format.
As far as I can tell, Google expects the URI to actually end in .png or be a real link versus a download URL you'd get from Google Drive.
Here is an example of the code I'm using to attempt to replace the image. The strImageObjectId is the objectId of the Embedded Object image in the template document copy that I want to replace. The Uri is what Google needs to pull the new image from. I'm happy to pull it from my local computer or Google Drive if only I could get it to accept it somehow.
BatchUpdateDocumentRequest batchUpdateRequest = new BatchUpdateDocumentRequest {
Requests = new List<Google.Apis.Docs.v1.Data.Request>()
};
request = new Google.Apis.Docs.v1.Data.Request {
ReplaceImage = new ReplaceImageRequest() {
ImageObjectId = strImageObjectId,
Uri = strChartWebContentLink
}
};
batchUpdateRequest.Requests.Add(request);
DocumentsResource.BatchUpdateRequest updateRequest =
sDocsService.Documents.BatchUpdate(batchUpdateRequest, strCopyFileId);
BatchUpdateDocumentResponse updateResponse = updateRequest.Execute();
I'm happy to use whatever method will get me to a point where I an end up with a Google Doc on Google Drive that was based on a template in which I can replace various text elements, but most importantly add/replace an image.
Thanks so much for the advice.
I got to the point were I believe I was specifying the URI correctly, but then I started getting an access forbidden error instead.
I didn't have time to hunt this one down, so I went back to creating an HTML template with my image, uploading as a Google Doc, exporting to PDF, and then uploading as a PDF. This ended up working because originally I was using a BMP as the file format and that is not supported by Google Docs, so I changed to a PNG instead and it worked just fine.
I think Google Docs needs to add the ability to add an image using a MemoryStream or some other programmatic base64 resource instead of purely being based on URIs and running into temporary upload or permission issues.
Hey I'm doing the same thing with you,
and I got this, by modify the download link format.
from this:
https://docs.google.com/uc?export=download&id={{YOUR GDRIVE IMAGE
ID}
to this
https://docs.google.com/uc?export=view&id={{YOUR GDRIVE IMAGE ID}
e.g :
uri: "https://docs.google.com/uc?export=view&id=1cjgyHqtYSgS0CBT4x-9eQIHRzOIfGgv-"
but the image should be set for public privilege
I want to let a user load an image from their gallery:
var imageIntent = new Intent();
imageIntent.SetType("image/*");
imageIntent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), REQUESTCODE);
This works fine: I can use the image in that activity. But I want to store the URI in the database so the image is associated with the object. I use:
_recipeImage.SetImageURI(data.Data);
model.ImageUrl = data.Data.ToString();
_recipeImage is just an image view I'm using to see if I actually got the image correctly for now.
When I save my model to the database I get a string that looks something like:
content://com.android.providers.downloads.documents/document/22
When the user opens this item later the image should load so I parse the string to a Uri. I try and set the image URI as I did before, but no image gets shown.
var uri = Android.Net.Uri.Parse(imageUrl);
imageView.SetImageURI(uri);
Is there a better/working way to do this?
I think you want to load image directly use URL, it's easier to use some third-party lib, for example Picasso.
Since it's a java lib, you can use Binding Library to use it in Xamarin.
After adding this lib into your project, you can simply code like this:
Picasso.With(this).Load(imageUrl).Into(imageView);
I have two identical images. One is saved as a .png in a folder, the other is saved as a SQLite blob in a database. I'm trying to figure out a good way to persist these images and show them in my view when needed.
I have two methods for trying to get these images into my view. The first one is reading the image files directly from the file system, converting them into a base64 string which I shove into a ViewBag. This method works fine.
The other method is trying to load the image blob directly from the database and then fetch the image from the Model in the view. This doesn't work so well.
Through a couple of breakpoints I've found out that there's a huge different between the base64 strings, depending on my method. I'm not sure why and what the difference between the SQLite image blob and the .png in my file system is, and any help would be appreciated since saving images in a folder seems like extra work, when I could just keep them in my database.
Loading from the file system:
public string FirstImagePath(string slideid, int well)
{
var firstPath = HttpContext.Current.Server.MapPath(slideid);
byte[] firstImageByteData = File.ReadAllBytes(firstPath + "_first" + well + ".png");
string firstImageBase64Data = Convert.ToBase64String(firstImageByteData);
string firstImageDataUrl = string.Format("data:image/png;base64,{0}", firstImageBase64Data);
return firstImageDataUrl;
}
Loading from SQLite db:
public byte[] FirstImage { get; set; }
public string FirstBase64Image
{
get { return ConvertImage(FirstImage);}
}
public string ConvertImage(byte[] imageBytes)
{
byte[] imageArray = imageBytes;
string imageBase64String = Convert.ToBase64String(imageBytes);
string imageDataString = string.Format("data:image/png;base64,{0}", imageBase64String);
return imageDataString;
}
Both methods have their advantages and disadvantages.
If you store your images in a DB it's very easy to move the application across different platforms without having to copy a lot of images. On the other hand your DB is going to get very large over time.
Storing images on disk allows you to overwrite them easily with a copy / paste job etc. However, if you have multiple web servers, you're going to have to make this change several times.
However, this isn't the main issue that I have with your approach. The problem is that you get the images, convert them to base64 and embed them in your output. This is bad practice for many reasons. Firstly, browsers are smart enough to load in multiple resources at a time. For instance, your browser can download multiple images at a time which will drastically increase page load speed. If you have the images embeded it becomes the responsibility of the html parser to render these images which in turn slows the overall download of the html document as it's larger.
I would recommend a hybrid of the two. Upload images to your database. Create an image handler in your application that retrieves an image from the DB and attempts to save it to the file system. If it's successful return this image. The next time you try to get this image (and it's not cached on the users machine) your handler can check the file system to see if it has already been created and return it.
This method gives you the advantages of both the DB and file structure methods. If you decide you need 10 web servers, all of your images will be stored in the DB, and the file system will be automatically populated with your images as and when you need them depending on which server receives the request. I recommend using a GUID or some unique id for your images.
You could even extend this further and pass height and width parameters to the image handler that will resize the image for you before sending it back to the client.
I bought a website template that has a scrolling photo gallery. As it came, the images are static in the fla file itself. I would like to edit the fla and load images dynamically. Ideally from MSSQL. I'm using VS2010, C# webforms, and SQL Server 2008 R2.
Are there any code snippets or tutorials or general guidance on how to do this? I do have a CS3 disc with Flash on it I can use for editing.
You can use a Loader + URLRequest, something like: (untested code)
var imgLoader:Loader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageHasBeenLoaded);
imgLoader.load(new URLRequest("imagePath/from/database.jpg"));
public function imageHasBeenLoaded(e:Event) {
//Get the loaded bitmap image, do what you want with it from here.
var img:Bitmap = Bitmap(e.target.content);
}
Of course you would also want to feed the file paths to flash, either by FlashVars or by hitting a web service type of page (or xml file) via a Flash URLLoader + URLRequest. I prefer an xml file myself.
This is a variation on a question that has been asked here several times. One example: Display an image contained in a byte[] with ASP.Net MVC3. The question is how to render an image from a byte array.
In all those questions, there is an Action similar to one of the answers in the link I provided above:
public FileContentResult Display(string id) {
byte[] byteArray = GetImageFromDB(id);
return new FileContentResult(byteArray, "image/jpeg");
}
With an image tag similar to this:
<img src="#Url.Action("Display", new { id = Model.Id })" />
This is done because it's not possible to send a byte array through a GET request, so just the id is sent allowing a lookup in the Action method. I get this part, it's not the problem. What I'm trying to do is create a 'Preview' page, where they can check their work before saving in the database. They can see the layout of the Title, Text and Image and decide whether to save, or go back and Edit some more. Therefore, the 'GetImageFromDB(id)' part won't work, since the object has not been saved yet to the database.
Is there any way to accomplish this, or do I just have to save the byte array temporarily in the database and access it that way for the Preview page?
You need to store the uploaded file somewhere on the server if you want to show it later (as a preview or full size image). Whether it is the database or the file system it is up to you. So once the file is uploaded you could store it as a temporary file on some location on the server by renaming it using some unique Guid and return this Guid to the client so that it can create an action link to a preview controller action passing the Guid which will fetch the file from the temporary location and stream it to the client.