Making SlideShowExtender retrieve images from MS Access database - asp.net

In my Microsoft access database I have a table called Picture and 3 fields: ID, Name, Image. I was wondering if it was possible to get all the images from the field 'Image' and display them into SlideShowExtender.
Are there any examples or explanations to do this?

I think you should use other slide show tools like "http://www.twospy.com/galleriffic/" It is so simple to use

If your images are stored in the Access database, then you will first have to create a handler to display them. This handler will get the bytes our of the Image column based on the ID and send the bytes back to the browser as an image. See this for an example:
Using ASHX files to retrieve DB images
With that in place you can display an image like this:
<asp:Image runat="server" ImageUrl="~/DatabaseImagehandler.ashx?id=42" />
The SlideShowExtender expects a service to provide it with an array of slides, so this service should basically return this:
// Based on SELECT COUNT(Id) FROM Images
int count = 5;
// Create the array of slides
AjaxControlToolkit.Slide[] slides = new AjaxControlToolkit.Slide[count];
// Put the URL and name for each image in the array
// Reader based on SELECT Id, Name FROM Images
int index = 0;
while(reader.Read()) {
string id = reader["Id"].ToString();
string name = reader["Name"].ToString();
string.path = Server.MapPath("~/DatabaseImagehandler.ashx?id=" + id);
// Create slide object with path set to point to the handler
AjaxControlToolkit.Slide slide = new AjaxControlToolkit.Slide(path, name, "img" + id);
slides[index] = slide;
index++;
}
return slides;

Related

NetSuite Advanced PDF Dynamically Created in Script - Cannot Set <img> tag

I am dynamically creating an Advanced PDF in script. I've created an XML string that I am then passing to NetSuite's XML to PDF API; nlapiXMLToPDF(xmlString).
I've added saved searches, tables, styling, and the xml string is parsing correctly.
I cannot add a logo in an tag as I'm not sure how to drill into the file cabinet and store the 'src' of the image.
Has anyone had experience dynamically creating Advanced PDFs in NetSuite and pulling in the logo in a script?
Are you trying to include an image from the file cabinet? If you have the internal ID of the file in the variable fileID, then you can use the following code:
var imageURL = nlapiLoadFile(fileID).getURL();
imageURL = nlapiEscapeXML(imageURL);
var xmlString = ... + '<div><img height="XXpx" width="XXpx" src="'+logoURL+'" /></div>' + ...;
var myPDF = nlapiXMLToPDF(xmlString);
If you want to use the Form Logo set on the Company Information page, then you can populate fileID using the following code:
var companyInfo = nlapiLoadConfiguration('companyinformation');
var fileID = companyInfo.getFieldValue('formlogo');
Then use the first code block to include the logo in xmlString.

save image on file system instead of DB

I m developping an application where user can provide text informations and images. I want to save images on file system and in DB, I will add a link of each user to its images So I need to add the user ID to the image name to not get images with same name. I m using SPRING MVC.
in my Controller :
#RequestMapping(value="/save",method=RequestMethod.POST)
public String add ( #RequestParam("prix") Long prix,
RequestParam("adresse") String ville,
#RequestParam("categorie") String categorie,
#RequestParam("photos") MultipartFile file,
) throws FileNotFoundException
{
String chemin=null;
if (!file.isEmpty())
{
try {
String orgName = file.getOriginalFilename();
// this line to retreive just file name
String
name=orgName.substring(orgName.lastIndexOf("\\")+1,orgName.length());
chemin="e:\\images\\"+name; //here I want to add id (auto generated)
File file1=new File(chemin);
file.transferTo(file1);
} catch (IOException e) {
e.printStackTrace();
}
}
annonce.setImage(chemin);
annonce.setTitre(prix);
annonce.setCorps(ville);
annonce.setPrix(cetegorie)
annoncedao.save(annonce);
return "SuccessAddAnnonce";
}
but I can not get the ID witch is auto generated so I can not get it throw #RequestParam like adress or categorie because it is auto generated and I still don t have it until we call save method witch is last .
Any suggestions are welcome .
I got the Id using query with Select max (id)
#Query("SELECT MAX(id) FROM Annonce")
Long findAutoincrementAnnonce();
I wish help someone.
You can generate a random ID for each user and assign it to a particular user, and thus its image name also (what you intend to do here I guess).
I will illustrate it simply.
// Get a random ID coressponding to the user address or whatever, here i will just use address
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(ville.getBytes("UTF-8"));
String randomId = DatatypeConverter.printHexBinary(hash);
// Here you have a uniqueID for an address.
// You could have just used ville.hashCode() also but it might give you collisions with some different Strings.
if Input: "64th Street, Downtown, StackOverFlow" then
randomId = 8AB126F8EBC0F7B5CCBBEB3E21582ADF

Retrieve multiple value in one session

when i add to cart
i using Session["Cart"] = new List() { Id }; which is get the id pass from the query string
but when i preview on the cartview event i add 2/3 product, it only will show 1 column, which is the latest. why it will replace? my code look like. how should i do to make sure every time i add it will be display on the view cart page? if this code problem or my cart preview page have probelm?
Try this example ,
string[] a = new string[]{"a","b","c"};
Session["values"] = a;
And you can retrieve it like this.
string[] a = (string[])Session["values"]
using List
Session["test"] = yourList;
And you can retrieve it like this.
var list = (List<string>)Session["test"];

What kind of data Type use to save an image in database

I need to insert images i have locally using SQL.
I'm doing it this way :
CREATE TABLE [dbo].[Table_1](
[SelectedImages] [image] NOT NULL,
[Path] [ntext] NOT NULL
)
to add an image, do this:
INSERT INTO [testing].[dbo].[Table_1]
([SelectedImages]
,[Path])
VALUES
('D:\desktop\05022006\free_chart1.gif' ,'D:\desktop\05022006\free_chart1.gif' )
The problem is i need to retrieve the image afterwards...
I use a Telerik control showing an image stored as binary data in a database
And i'm not sure the type "Image" is the correct one since it's not working...
<telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" DataValue='<%#Eval("Photo") %>'
AutoAdjustImageControlSize="false" Width="90px" Height="110px" ToolTip='<%#Eval("ContactName", "Photo of {0}") %>'
AlternateText='<%#Eval("ContactName", "Photo of {0}") %>' />
What kind of data type do you guys use to store an image ?
Thanks in advance
You can store images in the database using image type as you have it defined in your question. However, you need to convert the image into a byte array first.
string path = "D:\\desktop\\05022006\\free_chart1.gif"
using (System.IO.FileStream fstream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
// Create a buffer to hold the stream of bytes
byte[] buffer = new byte[fstream.Length];
// Read the bytes from this stream and put it into the image buffer
fstream.Read(buffer, 0, (int)fstream.Length);
fstream.Close();
//now you can insert buffer into the database
}
When you want to retrieve the image from the database, you have to convert it back from a byte[].
if (byteArray != null)
{
if (byteArray.Length > 0)
{
// Create a new MemoryStream and write all the information from the byte array into the stream
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray, true))
{
ms.Write(byteArray, 0, byteArray.Length);
// Use the MemoryStream to create the new BitMap object
Bitmap photo = new Bitmap(ms);
ms.Close();
//now you can use photo
}
}
}
What kind of data type do you guys use to store an image ?
It depends upon situation & its your call.
Sometimes just storing the URL of image inside a VARCHAR field would
suffice 'C:/Data/UserName/uploads/myGuid+filename.jpg' & actual file is present on disk (transaction management is difficult with this approach but works well for small requirements)
When transaction management is top priority you can store them inside database in VARBINARY(max) field Storing images in SQL
Server
SQL Server filestream is also an option with latest SQLServer on which the file is stored on filesystem managed by database SQL Server filestream Overview

An image from byte to optimized web page presentation

I get the data of the stored image on database as byte[] array;
then I convert it to System.Drawing.Image like the code shown below;
public System.Drawing.Image CreateImage(byte[] bytes)
{
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
return image;
}
(*) On the other hand I am planning to show a list of images on asp.net pages as the client scrolls downs the page. The more user gets down and down on the page he/she does see the more photos. So it means fast page loads and rich user experience. (you may see what I mean on www.mashable.com, just take care the new loads of the photos as you scroll down.)
Moreover, the returned imgae object from the method above, how can i show it in a loop dynamically using the (*) conditions above.
Regards
bk
Well, I think the main bottleneck is actually hitting the database each time you need an image. (Especially considering many users accessing the site.)
I would go with the following solution:
Database will store images with the original quality;
.ashx handler will cache images on the file system in various needed resolutions (like 32x32 pixels for icons, 48x48 for thumbnails, etc.) returning them on request and accessing database only once; (in this example is shown how to return an image via ashx handler)
The actual pages will point to .ashx page to get an image. (like <img scr="GetImage.ashx?ID=324453&Size=48" />)
UPDATE:
So the actual workflow in the handler will be like:
public void ProcessRequest (HttpContext context)
{
// Create path of cached file based on the context passed
int size = Int32.Parse(context.Request["Size"]);
// For ID Guids are possibly better
// but it can be anything, even parameter you need to pass
// to the web service in order to get those bytes
int id = Int32.Parse(context.Request["Id"]);
string imagePath = String.Format(#"images/cache/{0}/{1}.png", size, id);
// Check whether cache image exists and created less than an hour ago
// (create it if necessary)
if (!File.Exists(imagePath)
|| File.GetLastWriteTime(imagePath) < DateTime.Now.AddHours(-1))
{
// Get the file from the web service here
byte[] imageBytes = ...
// Save as a file
using (var memoryStream = new MemoryStream(imageBytes))
using (var outputStream = File.OpenWrite(imagePath))
Image.FromStream(memoryStream).Save(outputStream);
}
context.Response.ContentType = "image/png";
context.Response.WriteFile(imagePath);
}

Resources