Displaying multiple images with image control in asp.net 3.5 - c#

I want to display all the images from given directory. For that i have given image controls to display images, but i want to display images control with that url automatically as per images in that directory. Suppose in my directory 5 images are present then 5 image controls should be display in my button click event.
I have written code in button_click event that displays only two images from the given directory is as follows :
protected void btncompare_Click(object sender, EventArgs e)
{
Bitmap searchImage;
searchImage = new Bitmap(#"D:\kc\ImageCompare\Images\img579.jpg");
string dir = "D:\\kc\\ImageCompare\\Images";
DirectoryInfo dir1 = new DirectoryInfo(dir);
FileInfo[] files = null;
files = dir1.GetFiles("*.jpg");
double sim;
foreach (FileInfo f in files)
{
sim = Math.Round(GetDifferentPercentageSneller(searchImage, new Bitmap(f.FullName)), 3);
if (sim >= 0.95)
{
string imgPath = "Images/" + files[0];
string imgPath1 = "Images/" + files[1];
Image1.ImageUrl = "~/" + imgPath;
Image2.ImageUrl = "~/" + imgPath1;
Response.Write("Perfect match with Percentage" + " " + sim + " " + f);
Response.Write("</br>");
}
else
{
Response.Write("Not matched" + sim);
Response.Write("</br>");
}
}
}

Seems like you're trying to fit a square peg into a round hole here. The Image control is designed to show a single image.
If you want to display multiple images, use multiple image controls. I suspect that the reason you're not doing this is because you don't know how many images that you're going to need to display.
If I had the same problem, I would turn the contents of the image directory into something that could be bound onto a Repeater. Each ItemTemplate would contain its own Image control, allowing you to display as many images as you need without resorting to hackery.

Related

Set Dedault Picture For PictureBox And Save it C#

I want to know how i can get the picture from the project folder and if User not choice any picture i can save it as Default persoanl picture
i use this code for create name , path and save ( if i have any pic eveything as fine, if user not choice any picture my code broken
//Set Image Name
string imageName = txtNationalCode.Text.ToString() + Path.GetExtension(imgPersonalPhoto.ImageLocation);
//Set Image Path
string ImageLocation = imgPersonalPhoto.ImageLocation;
//Save Image
string path = Application.StartupPath + "/Images/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
if (imgPersonalPhoto.Image != null)
{
imgPersonalPhoto.Image.Save(path + imageName);
}
else
{
//i dont know how set some picture for default and save it ! ( i have 1 picture for background picturebox
imgPersonalPhoto.Image.Save(path + imageName);
}
}
catch
{
RtlMessageBox.Show("Add Picture for this Personal please ");
}
i will try add some picture in my app Folder
but if i send my app to other this folder not exist !
Set the image in Form_Load using Image.FromFile. This way it will always set the default image when the form is loaded.
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(#"C:\...");
}

Read Multiple Textfile upon Button Click and Display Content

I initially have a Fileupload tool to upload a textfile, manipulate its content and display into a Listbox or Textbox. The limitation however is Fileupload only supports single uploading, at least to the version of .Net Framework I am using.
What I intend to do is just use a button control and remove the Fileupload. Upon Button click I need to read the textfiles inside a designated folder path and display first the contents inside a multiple lined textbox. (not just the file name) This is my intially written codes, and it is not working.
protected void btnGetFiles_Click(object sender, EventArgs e)
{
string content = string.Empty;
DirectoryInfo dinfo = new DirectoryInfo(#"C:\samplePath");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
//ListBox1.Items.Add(file.Name);
content += content;
}
txtContent.Text = content;
}
Since your's is web based application you can't access physical paths like c:\\.. you should use Server.MapPath anyway(As per the comment, you don't need to get the file with Server.MapPath). Then for getting the content you can try something like the following:
protected void btnGetFiles_Click(object sender, EventArgs e)
{
try
{
StringBuilder content = new StringBuilder();
if (Directory.Exists(#"C:\samplePath"))
{
// Execute this if the directory exists
foreach (string file in Directory.GetFiles(#"C:\samplePath","*.txt"))
{
// Iterates through the files of type txt in the directories
content.Append(File.ReadAllText(file)); // gives you the conent
}
txtContent.Text = content.ToString();
}
}
catch
{
txtContent.Text = "Something went wrong";
}
}
you wrote content += content;, that is the problem. change it to content += file.Name;, it will work.

c# how to clone picture box 1 into pb 2?

I use two functions to load live camera and picture into picture box 1 and I want the same to be displayed in picture box2 also...
whatever comes in pb1 should also come in pb2.
Unfortunately, PictureBox does not have event like OnImageChanged, so you got to work around with it.
One way to work around with it is by creating your own MyPictureBox class derived from PictureBox (winform) in which it has its own (overshadowing) Image property. Then in the class you declare ImageChanged event and well as its handler. Then, in the setter of the Image property, you could call ImageChanged event.
When the ImageChanged occurs, you can change the other PictureBox image too.
Alternatively, you may want to make use of the existing (similar) LoadCompleted event of the PictureBox and then triggers the other PictureBox to get the new image.
See if any method may work for you.
Hi #user3004860 in my sample , I have quietly insert picture to the file folder when I put a picture in the box , so that you can use it in anywhere ,hopefully my idea can help you to deal with your question , as following code is the sample
if (!string.IsNullOrEmpty(fileExt))
{
var strExt = fileExt.ToLower();
var newFileName = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmss"), Random.Next(0, 9999).ToString("D4"), strExt);
var newOFileName = "o" + newFileName;
string imgUrl = "/Files";
var directory = new System.IO.DirectoryInfo(Server.MapPath("~"+imgUrl));
if (!directory.Exists)
directory.Create();
try
{
upload.SaveAs(directory + "/" + newOFileName);
string res;
res =
string.Format(
"top.$('.mce-btn.mce-open').parent().find('.mce-textbox').val('{0}{1}').closest('.mce-window').find('.mce-primary').click();",
imgUrl + '/', newOFileName);
return Content(res);
}
catch
{
return Content("error");
}
}

Adding image from folder to dropdown list

I want to add images from folder and list it in dropdown . Like my application has folder name flags containing all the flags images and their country name. how do I add them to dropdown .
Try using the System.IO.Directory.GetFiles and System.IO.Path.GetFileName
http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx
Something like (haven't tried it)
// Process the list of files found in the directory.
string [] files = Directory.GetFiles(yourDirectory);
foreach(string file in files) {
string language = Path.GetFileName(file);
ddlFlags.Items.Add(new ListItem(language, file));
}
Next time, improve your question by describing what you have tried so far, then it would be easier to help you.
You should include the System.IO namespace and add an ImageList to your form. Set its ImageSize to a nice size for you images.
Then use the code below to do the rest! It loads all files in a folder into both an ImageList and into the Items of a ComboBox. Note that it loads not the filenames but FileInfo objects, so that it can easily display the names without the path. Also note that to display images in a CombBox it has to be owner-drawn, which, as you can see it pretty straight-forward..
Here is the code to use & study:
using System.IO;
//..
// load whereever you like
// e.g. in the From.Load event or after InitializeComponent();
var images = Directory.GetFiles(yourImageFolder, "*.jpg");
foreach (string file in images)
{
imageList1.Images.Add(file, new Bitmap(file));
comboBox1.Items.Add(new FileInfo(file));
}
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += comboBox1_DrawItem;
comboBox1.ItemHeight = imageList1.ImageSize.Height;
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
FileInfo FI = (FileInfo)comboBox1.Items[e.Index];
e.Graphics.DrawImage(imageList1.Images[FI.FullName], e.Bounds.Location);
e.Graphics.DrawString(FI.Name, Font, Brushes.Black,
e.Bounds.Left + imageList1.ImageSize.Height + 3, e.Bounds.Top + 4);
}

Ajax ReordeList and asp:FileUpload Problem

I have a ReorderList which is working fine, inside the InsertItemTemplate, I've added a asp:Fileupload to add images to the list and database. All of these controls are inside a DIV.
How could I reach to this (asp:FileUpload) in C# to check whether it has a file or not,
this is the C# part of the code:
///////////////////////////////////////////////////////////////////////////////////////////////
protected void btnInsert_Click(object sender, EventArgs e)
{
string sFilename = Guid.NewGuid().ToString();
FileUpload filePhoto = (FileUpload)div1.FindControl("filePhoto");
if (filePhoto.HasFile)
{
string sPath = "";
string sFile = filePhoto.FileName.ToString();
sPath = Server.MapPath("Images");
filePhoto.SaveAs(sPath + "\\" + sFile);
//to fill the Notice image by code behine
ObjectDataSource1.InsertParameters["theImage"].DefaultValue = "Images\\" + sFile;
}
else
{
//to fill the Notice image by code behine
ObjectDataSource1.InsertParameters["theImage"].DefaultValue = "Images\\" + "NoImage.jpg";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
any ideas?
Thanks in advance
Actually ReorderList is an ajax control and you cannot use normal Asp:Fileuploader in ajax control. You have to use the asyncfileuploader control of ajax control toolkit in order to work in ajax application.

Categories

Resources