How to change picture file dimension in C#? - c#

I am working a mvc web app. I upload a image to use it as system logo. Now if I select image and upload it, it can be replaced easily until image file is not of large dimesion. For file with large dimension i need to reduce its size to some smaller size to make it look like a system logo. Preferable size for my logo is 100x75. How can I reduce the file dimesion?
THanks,
kapil

try this.
I found it in the msdn, and it works.
Bitmap map = new Bitmap(Image.FromFile("F:\\1.jpg"), new Size(20, 20));
map.Save("F:\\5.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

You can use the classes that are in the System.Drawing namespace.
See this tutorial for details.

Related

How to write on images using EmguCV in C#

I wanna write on images using EmguCV in Windows Form C# In actual I wanna create a simple app for images as an image editor. Its simple purpose is to open images through an open file dialogue I can open the file but now I can't get any help or resource on How to write on images?. Let me explain my question through some image examples:
Image Before Editing:
Image after Editing:
So how can I do this using EmguCV in C#
I am newbie in dealing with images if there any way so kindly tell me.
And If there is not a such function involved in writing on images using EmguCV then please tell the alternative if any of you know??
Thanks in advance.
You can write on images with CvInvoke.PutText, one simple example is here:
// load the image
Mat image = new Mat("test.png");
// write "hello!" in red(0,0,255) at 100,100 coordinate from the top left corner
CvInvoke.PutText(image, "hello!", new System.Drawing.Point(100, 100), FontFace.HersheySimplex, 1.0, new MCvScalar(0, 0, 255), 2);
// display the result
CvInvoke.Imshow("Image with text", image);
// wait for user input to dismiss the image
CvInvoke.WaitKey();
// free allocated memory
image.Dispose();
Seems confusing for somebody new to EmguCV but it really is just a matter of looking at what each parameters do, more info in the EmguCV official documentation.

Image manipulation in C#

I want to copy an image with all its parameters, including the manipulation data (scale, rotatio etc).
I'm creating a new image control and copy the source and size but can't find a way to set the same manipulation data.
I'm using System.Windows.Controls.Image and i have the ManipulationData object of the source image.
Have any idea?
Thanks
There's very good nuget package available for this..
https://www.nuget.org/packages?q=Tags%3A%22Magick.NET%22
For example to crop or resize an image:
MagickImage image = new MagickImage("filepath.jpg");
image.Crop(new MagickGeometry(424, 448, 224, 224));
image.Resize(123,234);
image.Write("output.jpg");
You can use RenderTargetBitmap to render any UIElement to bitmap:
The class exists in both, WPF and Windows Store Apps, however the usage is slightly different.
WPF Links:
- https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap(v=vs.110).aspx
Windows Store Links:
https://msdn.microsoft.com/library/windows/apps/dn298548
http://social.technet.microsoft.com/wiki/contents/articles/20648.using-the-rendertargetbitmap-in-windows-store-apps-with-xaml-and-c.aspx#Use_its_Pixels

Adding EXIF DateTime to a jpeg image

Problem:
WebEye.WebCameraControl makes an excellent job to capture a picture (Bitmap) in a WinForm App.
When saving the image (Bitmap): image.Save("filename.jpg", ImageFormat.Jpeg) it will not add any
exif data, my need is just datetime.
There are many exampels how to read PropertyItems from an existing jpg, change attributes and save the image. But I cannot find any that adds new propertyItems to an "exif-empty" image.
There is image.SetPropertyItem(propItem) that looks promising but the propItem it self cannot be instantiated but retrieved from an image image.PropertyItems[x] Yes, I do miss something here but what?
Please save my day. This issue is driving me nutts.
Best regards
Stephan

Cropping an image stored locally

I'm kind of a greenhorn to Windows Phone development and I've been looking for a way in order to crop an image already built into the project (Maybe even from the camera some day), but every package I've found has either had a fuss with Visual Studio, or throws argument exceptions. So I've decided that I will make my own function to do so.
However, I have not the slightest idea where to start. I'm pretty sure WriteableBitmap has something to do with it, and something to do with the following code:
Application.GetResourceStream(new Uri("/PhoneApp3;component/Assets/Flowers/Daff.jpg"));
So how would one start out with getting the pixel data or creating a new image and apply pixel data. And finally how would one save the result and reference it through the UI's image elements.
Use WriteableBitmapEx to crop images on Windows Phone. The way you solution will have to work is to manipulate the WritableBitmap.Pixels property. You first load in an image, change the Pixels property and transform the raw pixels into a saved image format like JPG or PNG. That's a lot of work so lucky for you WriteableBitmapEx does that for you.
First, install WritebleBitmapEx from NuGet:
Install-Package WriteableBitmapEx
Then you can load any image, crop it and save back to the MediaLibrary. Here's for example how to load a file from the app's XAP, crop to top-left 25% of the image and save to the "Saved Pictures" WP7/8 album.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var bmp = new WriteableBitmap(0, 0).FromContent("Assets/ApplicationIcon.png");
var croppedBmp = bmp.Crop(0, 0, bmp.PixelWidth/2, bmp.PixelHeight/2);
croppedBmp.SaveToMediaLibrary("myImage.jpg");
}
When we run this code we can see the new cropped image:
I recently needed to do this and didn't want to use an external lib. Microsoft provide a good example on MSDN on how to do it (and is also very good at not causing memory leaks!)
http://code.msdn.microsoft.com/wpapps/Photos-Sample-a38a2c8e
Cheers,
Will

Create Png in Windows8/C#

How I can create and save png-image in Windows8/C#? I find way without DirectX. Is there a way to render the set of XAML-primitives to png?
You can use the BitmapEncoder class to create a PNG image. Use the PngEncoderId as the Guid argument to CreateAsync. See this sample. See this sample for an example of how to save data.
There is no easy way to pickle the visual tree into an image like there is in Silverlight.

Categories