First, use folders and databases
Recently, I have done a lot about this feature. With the rapid development of the network, network access to images is no longer a myth, but it has become a fashion. If you are using Asp.net for development, more people may consider using a database to store pictures, while using folders to Store Pictures. There are two main methods for this method: how to read images, how to store images, and how to read images. I will not say that this is the easiest way to read images, you only need to give the address = to the object storing the image. Generally, you need to use the relative address, that is, "~". As shown below
ImageUrl="../CardDeal/SellCardZhi.jpg' ImageUrl="~/CardDeal/SellCardZhi.jpg'
Add the name of the folder on the server where your image is located.
Let's see how it is stored. One of my common methods is this.
/// <Summary> /// upload an image /// </Summary> /// <Param name = "fusshopurl"> fileupload object </param> /// <Param name = "upladurl"> name of the directory to which the image is stored </param> // <returns> If fileupload is not empty, the uploaded image location is returned, otherwise, an empty character </returns> Public static string uploadimage (fileupload fusshopurl, string upladurl) {If (fusshopurl. hasfile) {// obtain the current time. The first parameter is the image name string filename = datetime. now. tostring ("yyyymmddhhmmss") + datetime. now. millisecond. tostring (); // obtain the image extension string extent = system. io. path. getextension (fusshopurl. postedfile. filename); // rename the image filename + = extent; // set the string dir = system. web. httpcontext. current. server. mappath (upladurl); // specify the image path and file name string Path = dir + "\" + filename; // Save the uploaded image to the specified file and add the fusshopurl. postedfile. saveas (PATH); Return filename;} else {return "";}}
This method is used with the fileupload control. The method is easy to understand.
The returned method is a data that can be directly stored by a channel, and then called from the foreground.
The second method is to directly access the base64string code of the image.
This method is very convenient and can be converted directly. You do not need to write troublesome road Sutra problems.
Let's take a look at how it is stored in the database.
// Select the image private void button1_click (Object sender, eventargs e) {openfiledialog openfile = new openfiledialog (); openfile. title = "select the Longin image of the client"; openfile. filter = "Login image (*. JPG ;*. BMP; * PNG) | *. JPEG ;*. JPG ;*. BMP ;*. PNG | allfiles (*. *) | *. * "; if (dialogresult. OK = openfile. showdialog () {try {bitmap BMP = new Bitmap (openfile. filename); picturebox1.image = BMP; picturebox1.sizemode = pictureboxsizemode. zoom; memorystream MS = new memorystream (); BMP. save (MS, system. drawing. imaging. imageformat. GIF); byte [] arr = new byte [Ms. length]; Ms. position = 0; Ms. read (ARR, 0, (INT) ms. length); Ms. close (); // directly return this value and place it in the Data. PIC = convert. tobase64string (ARR);} catch {}}}
The reading method is also very simple. PIC is the image string we get, as long as we store it in the database and read it from the following method.
Note that I have added notes
// Load the image private void form1_load (Object sender, eventargs e) {try {// PIC = ........ this statement can be read from the database. // if it is null, if (! String. isnullorempty (PIC) {// convert the base64 code back to the array byte [] imagebytes = convert. frombase64string (PIC); // read the memorystream object memorystream = new memorystream (imagebytes, 0, imagebytes. length); memorystream. write (imagebytes, 0, imagebytes. length); // convert to image = image. fromstream (memorystream); // memorystream. close (); // do not add this sentence; otherwise it will be incorrect // place the image in picturebox this. picturebox1.sizemode = pictureboxsizemode. zoom; this. picturebox1.image = image ;}} catch {}}
Let's take a look at the results.
Here, you only need to click Select image to change it. These are very simple, but I personally think they are still very common, and there are a lot of examples on the Internet, but there are not many examples that can really help, because these methods are used in several of our projects, more or less, some employees do not know much about it. Here, I will post a post to help new users to check it. Haha
All the code in this example below
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. threading; namespace windowsformsapplication1 {public partial class form1: FORM {public form1 () {initializecomponent ();} string PIC = ""; // load the image private void form1_load (Object sender, eventargs E) {try {If (! String. isnullorempty (PIC) {byte [] imagebytes = convert. frombase64string (PIC); memorystream = new memorystream (imagebytes, 0, imagebytes. length); memorystream. write (imagebytes, 0, imagebytes. length); image = image. fromstream (memorystream); // place the image in picturebox this. picturebox1.sizemode = pictureboxsizemode. zoom; this. picturebox1.image = image ;}} catch {}// select the image private void button#click (Object sender, eventargs e) {openfiledialog openfile = new openfiledialog (); openfile. title = "select the Longin image of the client"; openfile. filter = "Login image (*. JPG ;*. BMP; * PNG) | *. JPEG ;*. JPG ;*. BMP ;*. PNG | allfiles (*. *) | *. * "; if (dialogresult. OK = openfile. showdialog () {try {bitmap BMP = new Bitmap (openfile. filename); picturebox1.image = BMP; picturebox1.sizemode = pictureboxsizemode. zoom; memorystream MS = new memorystream (); BMP. save (MS, system. drawing. imaging. imageformat. GIF); byte [] arr = new byte [Ms. length]; Ms. position = 0; Ms. read (ARR, 0, (INT) ms. length); Ms. close (); PIC = convert. tobase64string (ARR);} catch {}}}}}
Third, read binary data for access.
First, read the image into binary before processing it. This is fast and the code is relatively small. Thanks to the reminders and suggestions from the following netizens, here I will paste my simple code. How can I store the code in the database? You can write it by yourself. I only provide the access method.
Private void button1_click (Object sender, eventargs e) {openfiledialog openfile = new openfiledialog (); openfile. title = "select the Longin image of the client"; openfile. filter = "Login image (*. JPG ;*. BMP; * PNG) | *. JPEG ;*. JPG ;*. BMP ;*. PNG | allfiles (*. *) | *. * "; if (dialogresult. OK = openfile. showdialog () {try {// read as binary byte [] bytes = file. readallbytes (openfile. filename); // you can simply store the data in cmd. parameters. add ("@ image", sqldbtype. image ). value = bytes; // The output binary here puts the value obtained from the data here byte [] bytes = (byte []) model. image; picturebox1.image = system. drawing. image. fromstream (New memorystream (bytes); this. picturebox1.sizemode = pictureboxsizemode. zoom; // If saved as a file: file. writeallbytes (@ "D: \ text.jpg", bytes);} catch {}}}