MODI Document Create path - c#

I am currently working in an OCR reader using MODI dll and my code is like
MODI.Document md = new MODI.Document();
//image path
string fileToOCR="C:\\temp\\1in.jpg";
md.Create(fileToOCR);
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Image img = (MODI.Image)md.Images[0];
MODI.Layout layout = img.Layout;
layout = img.Layout;
string result = layout.Text;
md.Close(false);
And I want to use
//image path
string fileToOCR="C:\\temp\\1in.jpg";
to
//image path
string fileToOCR="http://example.com/image.png";
How this possible, please help me.

MODI needs a local file to work on. You can use WebClient.DownloadData (see http://msdn.microsoft.com/en-us/library/xz398a3f.aspx) to get the data, then save it locally.

Related

Xamarin Forms Load image from folder inside project

I have a problem. I am trying to save an Image to a folder in my project (not the Resources folder!) and load theimage from that folder into an Image holder as source. I want the image to be saved in a folder called: TempImages and my app name is MyApp. Here is the code I have now:
Saving:
using (var image = args.Surface.Snapshot())
using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
using (var stream = File.OpenWrite(Path.Combine("MyApp.TempImages", "CreatedImage.png")))
{
data.SaveTo(stream);
}
Opening:
string resourceID = string.Format("MyApp.TempImages.CreatedImage.png");
Assembly assembly = GetType().GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream(resourceID);
imgCanvas.Source = ImageSource.FromFile(resourceID);
But I think that File.OpenWrite a local file on my pc means, but I am not sure. And therefore I am not sure if I am opening the file correctly. Now I get the error that the save path doesn't exist.
How can I fix this?
you should be able to create any folder structure you want within one of the app writeable paths
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var folder = Path.Combine(path,"MySpecialFolder");
Directory.CreateDirectory(folder);
var file = Path.Combine(folder,"MyImage.png");
File.WriteAllBytes(file,data);
var image = File.ReadAllBytes(file);

Using a Resource Image as the value in RDLC Parameter

I'm trying to pass an image as a parameter to an Image in a RDLC Report. I tried using the following:
string imgPath = new Uri("pack://application:,,,/Resources/default_product_img.png").AbsoluteUri;
string imgPath = new Uri(AppDomain.CurrentDomain.BaseDirectory + "pack://application:,,,/Resources/default_product_img.png").AbsoluteUri;
string imgPath = new Uri("/Resources/default_product_img.png").AbsoluteUri;
string imgPath = new Uri(AppDomain.CurrentDomain.BaseDirectory + "/Resources/default_product_img.png").AbsoluteUri;
string imgPath = new Uri("pack://application:,,,/Resources/default_product_img.png", UriKind.Absolute).AbsoluteUri;
string imgPath = new Uri(HttpContext.Current.Server.MapPath("~/Resources/default_product_img.png")).AbsoluteUri;
string imgPath = new Uri(HostingEnvironment.MapPath("~/Resources/default_product_img.png")).AbsoluteUri;
but the display always show the red X when I run it. I managed to make this work, but the source of the image is in the same level as the .exe and not inside it.
I also tried creating a BitmapImage, but ReportParameter() only accepts strings.
Is there a way for this to work? Or should I just copy it beside the .exe file?
Things to Note:
The image source is set as External
default_product_img.png is inside Resources folder and has a Build Action of Resource
The parameter name is set as the value in Use this image:
Take the image as a bitmap and save it to a memory stream then convert the memory stream into a base64 string. Pass this string into the parameter and use that parameter as the image. In the RDLC set the image source to be database and make sure the mime type is a correct match for how you saved the bitmap to the memory stream.
string paramValue;
using (var b = new Bitmap("file path or new properties for bitmap")) {
using (var ms = new MemoryStream()) {
b.save(ms, ImageFormat.Png);
paramValue = ConvertToBase64String(ms.ToArray());
}
}
Or if you want to keep it as an external file set the image source to be external in the rdlc and pass the path to the image as file://c:\site\resources\default_product_img.png it will need to be an absolute path and you can use Server.MapPath to convert the web relative path to an absolute local path then just make sure you have file:// at the beginning of the path so the report engine knows it's a local path.

Image.ImageUrl = ... nothing is displayed

On my development system (Win 10 Pro, VS 2015), I am testing a web forms app in VS 2015; it uses a master page. An image control in an asp content page shows nothing when I try to display a photo from a file just written after a resize, whereas it displays the original photo just fine. Here is the code-behind fragment with the problem detailed in the comments:
// During testing, sRelLoc contains "~/i/SlideShow/1th.jpg" (original photo)
string sRelLocMod = sRelLoc.Replace("~/", "").Replace("/", "\\");
// During testing, Global.sgHomeDir contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\"
string sImgUrl = Global.sgHomeDir + sRelLocMod;
// sImgUrl now contains
System.Drawing.Image Photo = null;
// Read original photo from file
using (FileStream fs = new FileStream(sImgUrl, FileMode.Open, FileAccess.Read))
{
Photo= System.Drawing.Image.FromStream(fs);
}
// ResizeImage from https://www.codeproject.com/articles/191424/resizing-an-image-on-the-fly-using-net
System.Drawing.Image PhotoResized = ResizeImage(Photo, new Size(400, 400), true);
sImgUrl = Global.sgHomeDir + "i\\tempImg.jpg";
using (FileStream fs = new FileStream(sImgUrl, FileMode.Open, FileAccess.Write))
{
PhotoResized.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);
}
// NOTE THAT: The image has been saved correctly in its resized form inside the expected directory
sImgUrl = sImgUrl.Replace("\\", "/");
//sImgUrl now contains "K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg"
// Image1 is an Image control on an asp.net web page.
// PROBLEM: The following statement displays nothing where Image1 is
// placed (not even a red-x or anything) , ...
Image1.ImageUrl = sImgUrl;
// ... but displays the unresized original just fine when the following statement is used instead:
// Image1.ImageUrl = sRelLoc;
Same behavior whether or not running in debug mode.
Perhaps there is a better approach than writing the resized photo to temporary file tempImg.jpg and then reading from it?
Update after jignesh's and Sami's responses:
I am using MapPath in Default.aspx.cs (see new comments below).
Now using System.Uri to convert path to Url which starts with "file:///". Leads to nothing being displayed. Replacing "file:" with "http:" leads to a broken image symbol being displayed. Updated code is:
// During testing, sRelLoc contains "~/i/SlideShow/1.jpg" (original photo)
string sRelLocMod = sRelLoc.Replace("~/", "").Replace("/", "\\");
// During testing, Global.sgHomeDir contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP"
// which was obtained in Default.aspx.cs Page Load Event by
// Global.sgHomeDir = HttpContext.Current.Server.MapPath(null);
string sImgPath = Global.sgHomeDir + "\\" + sRelLocMod;
// sImgPath now contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\i\\SlideShow\\1.jpg"
System.Drawing.Image Photo = null;
// Read original photo from file
using (FileStream fs = new FileStream(sImgPath, FileMode.Open, FileAccess.Read))
{ Photo= System.Drawing.Image.FromStream(fs); }
// ResizeImage from https://www.codeproject.com/articles/191424/resizing-an-image-on-the-fly-using-net
System.Drawing.Image PhotoResized = ResizeImage(Photo, new Size(400, 400), true);
sImgPath = Global.sgHomeDir + "\\i\\tempImg.jpg";
// sImgPath now contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\i\\tempImg.jpg"
using (FileStream fs = new FileStream(sImgPath, FileMode.OpenOrCreate, FileAccess.Write))
{ PhotoResized.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); }
// I have verified that the image has been saved correctly in its resized form inside the expected directory
System.Uri ImgUrl = new System.Uri(sImgPath, UriKind.Absolute);
// Image1 is an Image control on an asp.net web page.
string sImgUrl = ImgUrl.ToString();
//sImgUrl now contains "file:///K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg"
Image1.ImageUrl = sImgUrl;
// The above statement DOES NOT work (displays nothing at all)
// However:
//Image1.ImageUrl = "~/i/tempImg.jpg"; // ** DOES ** work ok
// If I replace "file:" with "http:", a broken image symbol is displayed.
But what does work is using a tilde "~" even though it does not look like a Url. Is it ok to use the tilde? Will it get me into any trouble, like when deploying to a hosting server?
Use the ImageUrl property to specify the URL of an image to display in the Image control.You can use a relative or an absolute URL. A relative URL relates the location of the image to the location of the Web page without specifying a complete path on the server. The path is relative to the location of the Web page. This makes it easier to move the entire site to another directory on the server without updating the code. An absolute URL provides the complete path, so moving the site to another directory requires that you update the code.
By absolute url, they mean the entire IIS path to the URL (Not your disk directory path). (i.e. http://yourVirtualDirectory/ExternalImages/logo.jpg).
So here in above code sImgUrl="K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg" it should be like http://yourVirtualDirectory/tempImg.jpg

Get IsolatedStorage path for ShareMediaTask in WP8

I have been attempting to use a saved image in IsolatedStorage within the new ShareMediaTask in Windows Phone 8. I am having issues getting an image path from IsolatedStorage. I have successfully used the ShareMediaTask from the result of CameraCaptureTask as exampled in http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207027(v=vs.105).aspx but I am unsure of how to get the path from IsolatedStorage in wp8.
I was attempting to retrieved an image path using something of the following:
//Combine the directory and file name
filePath = Path.Combine(IsolatedStoragePath, fileName);
Uri uri = new Uri(#"isostore:" + filePath, UriKind.Absolute);
_shareTask = new ShareMediaTask();
//_shareTask.FilePath = #"isostore:" + filePath;
_shareTask.FilePath = uri.ToString();
_shareTask.Show();
Not sure if I'm headed in the right direction or not with this, any advice, assistance, or references would be greatly appreciated! The only similiar link I've found uses xna which I must avoid for this application http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/56c91aa1-26ea-41f7-b5ac-035537419faf/ .
I think the best you can do is to save the photo do MediaLibrary, share it and the delete it immediately after sharing.
your idea i working But after save image we are not able delete image because WP OS not give permission to delete other app item
var mediaLibrary = new Microsoft.Xna.Framework.Media.MediaLibrary();
var location = mediaLibrary.SavePicture(tempJpeg + ".jpg", e.Result);
string Path = location.GetPath();
ShareMediaTask SMT = new ShareMediaTask();
SMT.FilePath = Path;
SMT.Show();

C# mp3 ID tags with taglib - album art

Im making my own mp3 tagger, and everything is fine so far. Although im stuck reading the album art tag.
I would like to know how to display the cover in a C#.NET picture box, but everything iv seen about that particular tag is confusing me.
I know i can get tags from files like this
txtAlbum.Text = currentFile.Tag.Album;
but all i need to do is grab the picture from the file and whack it in a picturebox. Then i would like to know how to write a picture (jpg, png) into the file and overwrite the existing one.
Any help would be greatly appreciated, and thank you for your valued time.
Try this
TagLib.File tagFile = TagLib.File.Create(path);
IPicture newArt = new Picture(tmpImg);
tagFile.Tag.Pictures = new IPicture[1] {newArt};
tagFile.Save();
EDIT
var file = TagLib.File.Create(filename);
if (file.Tag.Pictures.Length >= 1)
{
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
PreviewPictureBox.Image = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(100, 100, null, IntPtr.Zero);
}
here's my quick and short solution for that problem:
var file = TagLib.File.Create(filename);
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
imageBox.Image = Image.FromStream(new MemoryStream(bin));

Categories

Resources