IPTC .NET read/write c# library - c#

I am looking for some library to read/write IPTC metadata from Jpg files.
Open source or paid, doesn't matter.
It should work with .NET 3.5 and c#.
Does anybody know such a library? I googled but haven't found anything.

*http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.aspx
using System;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;
namespace wictest
{
class Program
{
static void Main(string[] args)
{
var stream = new FileStream("1.jpg", FileMode.Open, FileAccess.Read);
var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
var metadata = decoder.Frames[0].Metadata as BitmapMetadata;
if(metadata != null)
Console.WriteLine(metadata.Keywords.Aggregate((old, val) => old + "; " + val));
Console.ReadLine();
}
}
}
You need to reference PresentationCore.dll and WindowsBase.dll to get access to the System.Windows.Media namespace.

Related

Unable to use fileInfo and fileStream in PCL project for Xamarin Project

I'm currently facing an issue with using fileInfo and fileStream related codes in my Xamarin PCL Project as Visual Studio will give an error saying that the type or namespace name 'FileInfo' could not be found.
I have already included the using System; and using system.IO statements in the code but it is still not working.
using System;
using System.IO;
using IPShare.Models;
using IPShare.ViewModels;
using Xamarin.Forms;
using System.Net;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using PCLStorage;
using System.Net.Http;
namespace IPShare.Views
{
public partial class ItemsPage : ContentPage
{
ItemsViewModel viewModel;
private async void OnButtonClicked(object sender, EventArgs e)
{
// Load file meta data with FileInfo
FileInfo fileInfo = new FileInfo(path);
// The byte[] to save the data in
byte[] data = new byte[fileInfo.Length];
// Load a filestream and put its content into the byte[]
using (FileStream fs = fileInfo.OpenRead())
{
fs.Read(data, 0, data.Length);
}
// Delete the temporary file
fileInfo.Delete();
}
}
}
As I'm new to xamarin and C# any help would be greatly appreciated.
The the forum thread linked to below says that FileInfo class is not available in PCL's (It's dated 2013, so this may be outdated).
How to use System.IO.File or similar for cross platform projects in PCLs?

What using statement do I need for WavReader and FlacWriter?

so far I have these using statements:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using System.Threading;
using NAudio.Wave;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Alvas.Audio;
I'm trying to write a wav to flac converter.
Here is the function:
public static void flac_converter()
{
string inputFile = Path.Combine("wav ", input);
string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac"));
if (!File.Exists(inputFile))
throw new ApplicationException("Input file " + inputFile + " cannot be found!");
WavReader wav = new WavReader(inputFile);
using (var flacStream = File.Create(outputFile))
{
FlacWriter flac = new FlacWriter(flacStream, wav.BitDepth, wav.Channels, wav.SampleRate);
// Buffer for 1 second's worth of audio data
byte[] buffer = new byte[wav.Bitrate / 8];
int bytesRead;
do
{
bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
flac.Convert(buffer, 0, bytesRead);
} while (bytesRead > 0);
flac.Dispose();
flac = null;
}
}
But I get this error: "The Type of Namespace 'WavReader' cannot be found"
I also get this error: "The Type or Namespace 'FlacWriter' cannot be found"
I know I have to do two things, download the appropriate lib, then all teh DLL files to the reference of the project and also add "using blahBlah;"
However, I don't know what DLL i need, or what using statements I need to get rid of these errors. Anyone know?
change the spelling of wavreader to wavereader. It should work out.

NAudio Object reference not set to an instance of an object

Referring to this question, I have used the code from it: Play audio from a stream using C#
However, I get an error "Object reference not set to an instance of an object" running in debug. I have assigned Url's to a combo box, and when I choose the first choice, opening an ASX stream, the program crashes, giving me the above in debugger.
Any ideas? I have tried doing some fixing with the code below, but I don't think I'm getting the idea, since it still isn't working.
Edit:// It doesn't recognize Mp3FileReader however there are no errors/warnings, it isn't green as it normally would be, am I missing a library?
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.Net;
using NAudio.Wave;
using NAudio;
public static void PlayMp3FromUrl(string url)
{
using (Stream ms = new MemoryStream())
{
using (Stream stream = WebRequest.Create(url).GetResponse().GetResponseStream())
{
byte[] buffer = new byte[32768];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
ms.Position = 0;
Mp3FileReader fr = new Mp3FileReader(ms);
WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(fr));
if(ms != null)
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
else
{
MessageBox.Show("blockAlignedStream variable was null!");
}
}
}
The code sample you are using is not a good example of how to play an MP3 from a stream. There are multiple problems with the code. Either use the technique described in this article, or you may be able to make use of the new MediaFoundationReader which can play from a URL.

Creating in-memory archive with multiple files [duplicate]

I know that the likes of the DotNetZip or SharpZipLib libraries are usually recommended for creating ZIP files in a .net language (C# in my case), but it's not impossible to use System.IO.Packaging to generate a ZIP file. I thought it might be nice to try and develop a routine in C# which could do it, without the need to download any external libraries. Does anyone have a good example of a method or methods that will use System.IO.Packaging to generate a ZIP file?
let me google this for you -> system.io.packaging+generate+zip
first link
http://weblogs.asp.net/jongalloway//creating-zip-archives-in-net-without-an-external-library-like-sharpziplib
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipSample
{
class Program
{
static void Main(string[] args)
{
AddFileToZip("Output.zip", #"C:\Windows\Notepad.exe");
AddFileToZip("Output.zip", #"C:\Windows\System32\Calc.exe");
}
private static void AddFileToZip(string zipFilename, string fileToAdd, CompressionOption compression = CompressionOption.Normal)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", compression);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
fileStream.CopyTo(dest);
}
}
}
}
}
}
In .NET Framework 4.5 you can use the new classes in the System.IO.Compression namespace.

Using System.IO.Packaging to generate a ZIP file

I know that the likes of the DotNetZip or SharpZipLib libraries are usually recommended for creating ZIP files in a .net language (C# in my case), but it's not impossible to use System.IO.Packaging to generate a ZIP file. I thought it might be nice to try and develop a routine in C# which could do it, without the need to download any external libraries. Does anyone have a good example of a method or methods that will use System.IO.Packaging to generate a ZIP file?
let me google this for you -> system.io.packaging+generate+zip
first link
http://weblogs.asp.net/jongalloway//creating-zip-archives-in-net-without-an-external-library-like-sharpziplib
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipSample
{
class Program
{
static void Main(string[] args)
{
AddFileToZip("Output.zip", #"C:\Windows\Notepad.exe");
AddFileToZip("Output.zip", #"C:\Windows\System32\Calc.exe");
}
private static void AddFileToZip(string zipFilename, string fileToAdd, CompressionOption compression = CompressionOption.Normal)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", compression);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
fileStream.CopyTo(dest);
}
}
}
}
}
}
In .NET Framework 4.5 you can use the new classes in the System.IO.Compression namespace.

Categories