I have defined a gridview control with a column of type ImageField and I bind the gridview to a List<MyRecord>.
MyRecord includes a property of type System.Drawing.Image, but the image is not rendered in the gridview.
There are numerous articles re binding using the URL field which points to (say) a jpg file... but i have the actual Image, not a file.
Any suggestions?
Create a custom ImageHandler like this article describes:
Image Handling In ASP.NET
That's not how the ImageField works. It expects the URL to the image.
<asp:ImageField DataImageUrlField = "ImageUrl" ...
The Handler.ashx worked... thank you
in the code, i set the ImageUrl = string.Format("~/HandlerGetImage.ashx?id={0}", imageNumber);
This dynamically calls the Handler in which i get the image index by
string v = context.Request.QueryString["id"];
Then, the following returns the image:
int ix = Convert.ToInt32(v);
MemoryStream ms = new MemoryStream(Query.imageList[ix]); // imagelist is created elsewhere
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
context.Response.ContentType = "image/jpeg";
//saving bitmap image
returnImage.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
returnImage.Dispose();
Related
This is the code I am facing problem .. in fetching uyearofjoin(datapicker) and picture from picturebox how to write code for it
uname.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
ufather.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
umother.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
usurname.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
uphone.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
uemail.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
ugrno.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
ucourse.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
urollno.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
uyearofjoin.Text = dataGridView1.SelectedRows[0].Cells[11].Value.ToString();
You want to display datetime and Image from dataGridView columns in a DateTimePicker and PictureBox controls respectively.
Let's assume that datetime is available in column 10 of DataGridView and Image is in column 11. The code to populate default value in DateTimePicker will be:
dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString());
Code to display image in PictureBox will be:
//Get the blob data in a variable
var data = (Byte[])(dataGridView1.Rows[e.RowIndex].Cells[10].Value);
//Create MemoryStream using blob data of image
var stream = new MemoryStream(data);
//Set the stream data to PictureBox
pictureBox1.Image = Image.FromStream(stream);
// e.RowIndex => selected row for which data is displayed in control
friends how to show image in proper size in stretch ..
..//Get the blob data in a variable
var data = (Byte[])(dataGridView1.Rows[e.RowIndex].Cells[10].Value);
//Create MemoryStream using blob data of image
var stream = new MemoryStream(data);
//Set the stream data to PictureBox
pictureBox1.Image = Image.FromStream(stream);
...image is not showing in proper size it is showing zoom how to resolve this error ....the code is perfect and working only this issuse i want image in proper format size like stretch
can anyone tell me the datepicker value from datagridview is not showing in my datepicker
showing only the default date value (8/10/2013)
and even when updating the datepicker the value is shown in my
datagridvierw (example 10/12/2015) updated but onclick of mouseclick event the value is not showing instead of updated date it is showing the default date value (8/10/2013)again
not showing updated one
I am trying to add an image in a DataGridView using the following code
DataGridViewImageColumn Editlink = new DataGridViewImageColumn();
Image image = Image.FromFile("Images\\Edit.png");
Editlink.Image = image;
Editlink.HeaderText = "Edit";
Editlink.DataPropertyName = "lnkColumn";
Editlink.Width = 40;
In the above code
Image image = Image.FromFile("Images\\Edit.png");
It throws an error saying
File not found
When I changed the FromFile path to "C:\\Test\\Images\\Edit.png", it works.
How can I achieve the same result without using actual path?
Please use Add Resource to add Images to local Resource folder and then call it by file name.
The question is a duplicate of this
filenotfound-when-i-use-image-fromfile
I'm new in this field. I'm working in windows application, I create panel and adding the buttons inside the panel. i want to retrieve the image from database and set to the background image in button.
This is my code,
FileName = objDR["Photopath"].ToString();
byte[] data = Encoding.UTF8.GetBytes(FileName);
MemoryStream ms = new MemoryStream(data);
image = new System.Drawing.Bitmap(ms);
Buttons[i].BackgroundImage = image;
initially put BackgroundImage property ON
OR
You can Code :
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));
I have an image tag on the page as shown below
<asp:Image ID="imgbadge" runat="server" Height="200px" Width="200px" />
and later based on the selection on the drop down box i need to assign the respective images to that image control.
I tried doing this
if (Session["BadgeType"].ToString() == "HAPPY BIRTHDAY")
{
imgbadge.ImageUrl = Server.MapPath("images/HappyBirthdayBadge.png");
}
else
{
imgbadge.ImageUrl = Server.MapPath("images/ServiceAnniversary.png");
}
But i am not getting the image displayed on the page.I tried checking on the web but they are explaining about using handlers. where i am not getting complete details required.
Thanks,
Sai krishna
Try this:
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ImageUrl = Server.MapPath("images/ServiceAnniversary.png");;
You need to give the virtual path of image and not the physical path, try this:-
imgbadge.ImageUrl = "~/images/HappyBirthdayBadge.png";
This should display your image properly.
I have an image control which is showing the preview image. If the user delete the image (which resides in folder) it should show the newly taken image.
but the image control shows the deleted image instead of showing new image.
// clear the image source before deleting the image.
// save image in the directory
public string globalFilePath;
int imageCount = Directory.GetFiles(imgDir, "*", SearchOption.TopDirectoryOnly).Length;
string filePath = Path.Combine(imgDir, "IMAGE_" + ++imageCount + ".jpeg");
globalFilePath = filePath;
// setting image control source
var strUri = new Uri(WebCamControl.globalFilePath, UriKind.Relative);
previewImage.Source = BitmapFromUri(strUri);
//Method
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
// delete image
previewImage.Source = null;
if (System.IO.File.Exists(WebCamControl.globalFilePath))
{
System.IO.File.Delete(WebCamControl.globalFilePath);
}
else
{
MessageBox.Show("File Not Exists");
}
After deleting the image in the directory file the image image control should show the new image, but my Image control shows the deleted image. please give me the solution.
In WPF, we generally don't need to use actual BitMapImage objects to display an Image. It's far easier to let the .NET Framework convert our string file paths to the images to the actual Image elements.
Also, it is far better to data bind the Image.Source to a property that implements the INotifyPropertyChanged interface (or a DepenedencyProperty):
<Image Source="{Binding YourImageFilePath}" ... />
Then, whenever a file path of a new image is set to the YourImageFilePath property, your displayed image will update immediately.
YourImageFilePath = filePath;
Try This one:
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.SetValue(System.Windows.Controls.Image.SourceProperty, null);
File.Delete(path);
OR
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.Source = null;
File.Delete(path)