For some reasons, I need to write few image processing functions without using GDI+. I need to be able to do the following operations on images.
Draw some shapes on existing images (mostly rectangle with a plain background color). Regularly i do this using Graphics.DrawImage() function in GDI+.
Draw texts on existing image. Regularly I do this using Graphics.DrawString() function in GDI+
Save image to MemoryStream. Regularly I do this by Image.Save(stream, imgformat) function in GDI+
Get image bytes. Regularly I do this by MemoryStream.ToArray() function. I need bytes because I need to be able to send image to HttpContext() using the context.Response.BinaryWrite(imageBytes) method.
I've already looked at AForge.net, but it is missing Image.Save() method. It uses native Bitmap.Save() method of GDI+. So I seem to have no way of surviving GDI+ with AForge.
I've also looked at OpenCV and its .NET wrapper Emgu, but after 2 days of hard try, I was not able to successfully integrate the project into my own project (I know this sound silly, but that is truth. Following all the tutorials, probably 20+ SO posts on this did not help, because my solution structure is a little bit more than (more complex) a regular solutions).
How can I achieve this? Show me some way please. Have you even found yourself in such a situation (situation where you need to process images, but no GDI+)? What are the other libraries that could help me?
There's a recent project that's gathering momentum called, appropriately, "ImageProcessor": https://github.com/JimBobSquarePants/ImageProcessor - NuGet reports over 344,000 downloads as of June 2016.
Scott Hanselmann did a feature on it about 8 months ago, explaining the motivation given that .NET Core does not have System.Drawing (and I note that Windows Azure does not officially support GDI in Website Roles): http://www.hanselman.com/blog/RFCServersideImageAndGraphicsProcessingWithNETCoreAndASPNET5.aspx
I just attempted to answer a similar question here:
server-side graphics generation in .net/c#
Adding any other vector graphics (and layout) would be quite simple as well. Let me know if you would like a sample project or code.
Related
I've got some C# code in Unity that grabs a large image from my Android Device's filesystem, and now with it I want to use it to create a small thumbnail image.
I've found lots of different suggestions for how to do this such as the following:
MemoryStream outputStream = new MemoryStream();
System.Drawing.Image image = System.Drawing.Image.FromFile(originalImagePath);
System.Drawing.Image thumbnail = image.GetThumbnailImage(thumbnailWidth, thumbnailHeight,()=>false, IntPtr.Zero);
thumbnail.Save(outputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return outputStream;
However, as with the above method, all of the methods I've found require you to use the System.Drawing namespace. And I can't for the life of me get the functions in this namespace to work on Android, because even after adding "System.Drawing.dll" into the Assets folder, I get an error saying that it can't locate "gdiplus.dll" on construction of "System.Drawing.Image". I tried downloading and adding said "gdiplus.dll" to Assets, but I just get the same error as if it can't find it!
I don't understand why its so hard to get the System.Drawing functions working in Unity, but that's somewhat besides the point, as all I really want to do is create a thumbnail of an image that lives on the user's Android Device. Any suggestions would be welcome!
EDIT: I forgot to mention that I'd like to avoid solutions that use Texture2D's because they can't be run off the main thread, and hence come with performance consequences =(
Thanks in advance! =)
Don't use Texture2D.Resize, because the resulting texture will become grey:
After resizing, texture pixels will be undefined.
See this post for other solutions.
Old answer
I recommend use an opensource C# image processing library, such as ImageSharp or search it on GitHub.
The last solution is to write one yourself, if the performance or size of a 3rd-party library is still not good enough.
Here are two ideas I can come up with:
split the possibly lengthy reading and resizing procedure into several Coroutines.
use multi-thread
You may also try calling java functions from Unity3D. But I'm not familiar with that.
I want to create my own Google Map like this:
My problem is that I can't load and edit my large images.
My Images:
PNG / JPG
700 MiB
300000px x 300000px
My attempts:
ImageMagick
.NET C# / BitmapImages ...
C++ / OpenCV
general image classes in Java and Python
With which language / library I can edit these big images.
I help maintain libvips, an image processing library designed to work with very large images. It's free and works on Linux, Mac and Windows. You can use it from the command-line, C#, C/C++, Python, Ruby and others.
You can make your google maps tiles from the command-line like this:
vips dzsave hugefile.tif myoutputdir --layout google
Or from Python (for example) like this:
import pyvips
image = pyvips.Image.new_from_file("somehugefile.tif", access="sequential")
image.dzsave("filename/of/pyramid", layout="google")
And it'll scan your huge tiff image and generate all the tiles. It's fast, it needs little memory and it'll work on images of any size. I regularly make 200,000 x 200,000 deepzoom images from microscope slides using my small laptop.
There's a chapter in the libvips docs introducing dzsave and explaining how to use it.
This is not a full answer, but I need a little more space than a comment can give.
Take a look at the large image support section on the ImageMagick or the discussion board.
This answer mentions the VIPS package which might be helpful.
You might also consider posting in photography stackexchange, or even blender stackexchange - for example I saw this answer which mentions writing individual image tiles - also here, although that question is about rendering. Blender is not specifically for image processing and editing, but it's pretty amazing and flexible and has a very active and supportive community. You can use python within Blender as well.
You could also think of asking in gis stackexchange.
When you post in the other stackexchanges, take a look around first and make sure you write your question so that it does not look too off-topic for that site.
Good luck - it seems tiling is everywhere!
hi
I am developing a video capture application using C#.net. i captured
video through webcam and saved it as a JPEG images then i want to make a
wmv file with those images. how can i do that what are the basic steps needed for that can any body help
I am working on this myself. I have found two ways that may be possible - both require the purchase of an outside library.
The first one looks to be the easiest but costs the most, although it will allow you to use it for free you will just have to deal with a pop up telling you to purchase the library: http://bytescout.com/products/developer/imagetovideosdk/imagetovideosdk_convert_jpg_to_video.html
The other involves using Microsoft Encoder 4. I am still working on this one. You can get the free version here: http://www.microsoft.com/download/en/details.aspx?id=18974
C# doesn't natively support much in the way of sound or video so outside reference assemblies seem to be a necessity.
Right now that is the best help I can offer until I figure it out.
I searched on how to check if a TIFF file is corrupt or not. Most suggests wrapping the Image.FromFile function in a try block. If it throws an OutOfMemoryException, its corrupt. Has anyone used this? Is it effective? Any alternatives?
Please check out the freeware called LibTiff .NET. It has the function to check if every page in a TIF file is corrupted or not. Even partially corrupt also no problem
http://bitmiracle.com/libtiff/
Thanks
Many tiff files won't open in the standard GDI+ .NET. That is, if you're running on Windows XP. Window 7 is much better. So any file which is not supported by GDI+ (i.e. fax, 16 bit gray scale, 48bpp RGB, tiled tiff, piramidical tiled tiff etc.) are then seen as 'corrupt'. And not just that, anything resulting in a bitmap over a few 100 MByte on a 32-bit system will also cause an out-of-memory exception.
If your goal is to support as much as possible of the TIFF standard, please start from LibTiff (derivates). I've used LibTiff.NET from BitMiracle (LGPL), which worked well for me. Please see my other posts
Many of the TIFF utilities are also based on LibTIFF, some of them are ported to C#.NET. This would be my suggestion if you want to validate the TIFF.
As for the TIFF specification suggested in other replies: of course this gives you bit-level control. But to my experience you won't need to go that low to have good TIFF support. The format is so versatile that it will cost you an enormous amount of time to start support from scratch.
It will only be corrupt in the sense that the frameworks methods cant open it.
There are some TIFF types that the framework cannot open -( In my case I cant remember the exact one, think it was one of the FAX type ones...)
That may be enough for you, if you are just looking a using the framework to manipulate images. After all I you cant open it, you cant use it...
ImageMagic - may give you more scope here
Without looking at the tiff, it may be difficult to see if its corrupt from a visual perspective, but if you have issues with processing an image, just create a function that does a basic test for this type of processing and handle the error?
Is there a way to draw an emf metafile (exported form a drawing tool) with antialiasing enabled? The tools I tried are not capable of exporting emf files antaliased so I wondered if I can turn it back on manually when drawing the emf in the OnPaint override of my Controls.
If anyone can confirm that is technically possible to generate antialiased emf files, another solution would be to use a drawing tool that can export to antialiased emf or have a 3rd party converter do this later. If anyone knowns such a tool, please let me know.
EDIT: When looking at the emf instructions it doesn't seem that emf itself can actually store the information whether it is to be rendered antialiased or not. At least I couldn't find anything. It is more likely that the antialiasing is done by the playback engine. For example when I open an emf in Word 2007 it is rendered antialiased. But not when I draw it with GDI+ "playback engine" (Graphics.DrawImage(...)). or when I view it the standard windows image viewer.
This makes me believe that some tools actually have their own emf playback engine. So maybe there is free .NET library (preferably with source code) that give me an object model of the emf instructions stored in the parsed emf file so I can play it back myself instead of using Graphics.DrawImage(...)?
We had a similar issue in a DirectX project. Upscaling and downscaling works to a certain degree, but it's faking it. If it's something you need to do over and over, you could perhaps parse the records of the WMF and draw them with GDI+ antialiased.
The following threads back this up (but they're from 2005 so things might have changed):
http://www.dotnet247.com/247reference/msgs/28/144605.aspx
http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-sdk/1127/Graphics-DrawImage-metafile-no-antialiasing
[Edit:]
These three programs might do the job for you: I'm assuming you're ok with doing it by hand:
http://emf-to-vector-converter-command-line-ser.smartcode.com/info.html
http://www.verypdf.com/pdf-editor/index.html
http://www.ivanview.com/converter/emf-batch-converter.html
[Edit II:]
Well, here's a program that will let you inspect an EMF in various ways:
http://download.cnet.com/windows/3055-2383_4-10558240.html?tag=pdl-redir
...and here's a freeware library that will let you parse 122 of the EMF commands and output them in GDI+. That should probably do the trick:
http://www.codeproject.com/KB/GDI-plus/emfexplorer.aspx?msg=2359423
...oh, and notice also comment #3 on the codeproject page. Looks like someone have banged their heads against the wall before. Hope this solves your problem.
EMF is using GDI commands, not GDI+, so it has no notion of antialiasing. I suspect that when you ask GDI+ to render the file, it sends it to GDI and just copies the resulting bitmap.
Duplicating this in code would be the same as reimplementing GDI, so it's not terribly feasible. Not impossible, just a larger job than the benefit would justify. If there is an open source utility that can open EMF files outside of Windows, you might look into the source code.
My guess is that Word is using the downsampling trick.
EMF file is a list of GDI commands. So it won't be anti-aliaised, even if under GDI+, you put a SmoothingMode() call before the drawing. You'll have to enumerate the GDI commands, then translate it into GDI+ commands.
Under Vista/Seven, you can use GDI+ 1.1 function named GdipConvertToEmfPlus/ConvertToEmfPlus. If you want your program to work with XP, you should write your own enumeration, then conversion to GDI+ commands.
The GDI enumeration then conversion to GDI+ is possible has been done by emfexplorer, but I've written some code perhaps more easy to follow, even if it's written in Delphi.
I'm putting this answer just now (I'm late), because I spent a lot of time finding out a solution using ConvertToEmfPlus, and writing some tuned open source code in case this method is not available.