I've looked on the web and it looks like an extremely simple solution may not be possible, but I'm hoping someone here knows something I have not found.
I have an app that displays info about all JPEG images in a directory in a GridView. Relevant bits of code and a screen shot are at the bottom.
This works just dandy, but I need to add "Date Taken" to this GridView for any image that happens to have that metdata. Is there a relatively simple example of this or am I in over my lightweight head?
Yeah, this does it. My next trick is to figure out how to include this in the table. It's no big deal to bind a GridView and DirectoryInfo, but I don't know how I'll include this other data in the table.
Try doing this you can bind anything to a backend method just like you can to anything else:
// C# Code Behind
public string GetDateTaken(string file)
{
FileInfo f = new FileInfo(file); // Move the fileinfo creation into the function itself
FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
BitmapSource img = BitmapFrame.Create(fs);
BitmapMetadata md = (BitmapMetadata)img.Metadata;
string date = md.DateTaken;
return date;
}
// ASPX Page
// Add this to your Gridview whrever you need it.
<asp:TemplateField HeaderText="Date Taken">
<ItemTemplate>
<asp:Label ID="DateTakenLbl" runat="server" Text='<%# GetDateTaken(Eval("Name").ToString()) %>' />
</ItemTemplate>
</asp:TemplateField>
BOOM! I had to change Name to FullName in the ASPX, but it works so well. Man, this is awesome. You're going to make me look like quite the rock star to a particular department in this building.
Glad it worked out for you! Yes my bad sorry about that. Name is just the friendly filename, but FullName is the full file path used in the FileInfo class. Have fun being a hero!
mrcoulson
Member
5 Points
31 Posts
"Date Taken" JPEG metadata
Nov 05, 2012 05:56 PM|LINK
I've looked on the web and it looks like an extremely simple solution may not be possible, but I'm hoping someone here knows something I have not found.
I have an app that displays info about all JPEG images in a directory in a GridView. Relevant bits of code and a screen shot are at the bottom.
This works just dandy, but I need to add "Date Taken" to this GridView for any image that happens to have that metdata. Is there a relatively simple example of this or am I in over my lightweight head?
Jeremy
// C# void getInfo(string strDir) { DirectoryInfo dirDir = new DirectoryInfo(Server.MapPath(strDir)); gvFiles.DataSource = dirDir.GetFiles().Where(x => (x.Attributes & FileAttributes.Hidden) == 0).Where(s => s.Extension == ".jpg" || s.Extension == ".jpeg"); gvFiles.DataBind(); gvFiles.Visible = true; } protected void btnSubmit_Click(object sender, EventArgs e) { getInfo(txtDir.Text.Trim()); }<%-- ASPX --%> <asp:GridView runat="server" ID="gvFiles" Visible="false" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Name/Link"> <ItemTemplate> <asp:HyperLink ID="lnkLink" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# Eval("Name", "img/" + "{0}") %>' /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="LastWriteTime" HeaderText="Last Written" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}"> </asp:BoundField> <asp:BoundField DataField="Length" HeaderText="Size" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,### bytes}"> </asp:BoundField> </Columns> </asp:GridView>N_EvilScott
Star
8179 Points
1466 Posts
Re: "Date Taken" JPEG metadata
Nov 05, 2012 06:22 PM|LINK
Try this code taken from the following link:
http://stackoverflow.com/questions/2280948/reading-data-metadata-from-jpeg-xmp-or-exif-in-c-sharp
public string GetDate(FileInfo f) { FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); BitmapSource img = BitmapFrame.Create(fs); BitmapMetadata md = (BitmapMetadata)img.Metadata; string date = md.DateTaken; return date; }EDIT: You don't need the Console.Writeline() bit in there... forgot to mention that :P
mrcoulson
Member
5 Points
31 Posts
Re: "Date Taken" JPEG metadata
Nov 05, 2012 07:35 PM|LINK
This looks promising. Give me a little time to figure out how to use it exactly :) I'll be back!
mrcoulson
Member
5 Points
31 Posts
Re: "Date Taken" JPEG metadata
Nov 05, 2012 07:48 PM|LINK
Yeah, this does it. My next trick is to figure out how to include this in the table. It's no big deal to bind a GridView and DirectoryInfo, but I don't know how I'll include this other data in the table.
Any great suggestions?
N_EvilScott
Star
8179 Points
1466 Posts
Re: "Date Taken" JPEG metadata
Nov 05, 2012 08:03 PM|LINK
Try doing this you can bind anything to a backend method just like you can to anything else:
// C# Code Behind public string GetDateTaken(string file) { FileInfo f = new FileInfo(file); // Move the fileinfo creation into the function itself FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); BitmapSource img = BitmapFrame.Create(fs); BitmapMetadata md = (BitmapMetadata)img.Metadata; string date = md.DateTaken; return date; }// ASPX Page // Add this to your Gridview whrever you need it. <asp:TemplateField HeaderText="Date Taken"> <ItemTemplate> <asp:Label ID="DateTakenLbl" runat="server" Text='<%# GetDateTaken(Eval("Name").ToString()) %>' /> </ItemTemplate> </asp:TemplateField>mrcoulson
Member
5 Points
31 Posts
Re: "Date Taken" JPEG metadata
Nov 05, 2012 08:37 PM|LINK
BOOM! I had to change Name to FullName in the ASPX, but it works so well. Man, this is awesome. You're going to make me look like quite the rock star to a particular department in this building.
THANKS!
Jeremy
N_EvilScott
Star
8179 Points
1466 Posts
Re: "Date Taken" JPEG metadata
Nov 05, 2012 08:39 PM|LINK
Glad it worked out for you! Yes my bad sorry about that. Name is just the friendly filename, but FullName is the full file path used in the FileInfo class. Have fun being a hero!