Flip C# Telerik image from being upside down - c#

I am attempting to past an image onto a pdf using Telerik in C#. The following code adds the image, but it's upside down. Telerik seems to paste it upside down no matter what rotation type I use on RotateFlip. The image should display UnUnU, but it's upside down and displays NuNuN. I added the image into the below code in base64.
How can I flip the image?
using (MemoryStream ms = new MemoryStream((byte[])Convert.FromBase64String("/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAR6ADAAQAAAABAAAAFAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAFABHAwERAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB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Image(new ImageSource(ms));
}
Thanks for any help!!

Related

Saved png image lose transparency when inserted in Catia V5 CATDrawing

I have a problem with transparency in the .png image when inserting to Catia V5 CATDrawing document. Something changes in the image after saving with System.Drawing.Image.Save(). If a newly saved image is opened with Paint.NET image is transparent and everything seems fine, but when inserted in CATDrawing image has no transparency. I can't find out what is changed in the image to cause this loss of transparency.
I'm rotating image that's why I need to save it. This is the code I'm using to rotate and save images.
Bitmap image1 = (Bitmap)Image.FromFile(sImagePath1, true);
image1.RotateFlip(RotateFlipType.Rotate90FlipNone);
image1.MakeTransparent();
image1.Save(sImagePath2, ImageFormat.Png);
I have also tried and failed with NuGet package Magick.NET-Q16-AnyCPU
using (MagickImage mimg = new(sImagePath1))
{
mimg.Rotate(90);
mimg.Write(sImagePath2);
}
and with save as stream
Bitmap image1 = (Bitmap)Image.FromFile(sImagePath1, true);
image1.RotateFlip(RotateFlipType.Rotate90FlipNone);
using (FileStream outputFileStream = new(sImagePath2, FileMode.Create))
{
var stream = new MemoryStream();
image1.Save(stream, ImageFormat.Png);
stream.Position = 0;
stream.CopyTo(outputFileStream);
}
Question: is there any other solution to rotate and save images via C# code?
Hi I'm afraid that Catia V5 can't import png with transparency. Try it yourself if manually putting the image into drawing keeps transparency. I Tried it but in my case it doesn't work.
In Catia you can rotate the picture manually but not trough api.
You can try vector image instead of bitmap, but this is just my guess.

WPF image control and jpg rotation metadata

I am having some trouble with Image control in WPF.
I have one jpg file, which loads with wrong rotation and even i rotate this picture in windows (right click and rotate left/right) there is no change in application.
Seems that there are some EXIF metadata in the image, which gets rotated together with a wrong image.
I'm reading the image from www so I do not have local file (and I don't want to have it). Here's how I'm converting byte[] to BitmapImage:
public static BitmapImage BitmapImageFromByteArray(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
stream.Close();
stream.Dispose();
return image;
}
So there are 2 ways of handling that:
Set Image control to ignore EXIF metadata
Remove EXIF metadata from BitmapImage
Can you help me with handling any of these?
Check out the code sample on the following link.
Remove Exif data from image files with C# and WPF libraries: http://www.techmikael.com/2009/07/remove-exif-data-from-image-files-with.html
Another option may be to use a RotateTransform to rotate the Image element:
How to do rotation around control's center in XAML

Lumia imaging crop filter

Hi so I'm making an app for Windows 10 that requires a user to choose an image and it's going to crop the image to 310*128. I got the file picker code already. But I want to know how to actually crop and save the image and display in image box. I already have the xaml page done
With Lumia Imaging SDK you can both crop the selected image and resize the image. In this answer I assume you actually want to crop, but from the text I could just as well guess you really just want to resize.
For crop, use the CropEffect from Lumia.Imaging.Transforms. Set the CropArea property on it to the object, and then render it. If you are rendering straight to the XAML page I recommend using a SwapChainPanel object in XAML and a SwapChainPanelRenderer to render on it.
Given that you are loading a StorageFile and rendering to a SwapChainPanel your code might look like something like this:
StorageFile file = ...
using (var source = new StorageFileImageSource(file))
using (var crop = new CropEffect(source, new Rect(0, 0, 310, 128))
using (var renderer = new SwapChainPanelRenderer(crop, YourSwapChainPanel))
{
await renderer.RenderAsync();
}

How to draw vector graphics and save it in PDF file?

I'm working on a project which generally is collecting data and drawing results in charts. (Using C#) I need to save my charts in a PDF file. My question is, how to save charts in a PDF file without loosing resolution? My point is how to draw vector graphics instead of raster graphics?
I tried iTextSharp to create PDF file but the result is not satisfying at all!
I'm new here, so I'm not able to upload pictures.
Here is the result after saving my file:
https://www.dropbox.com/s/ruwtc82hfosxk6y/Test.pdf?dl=0
Here is the PDF that I need to create:
https://www.dropbox.com/s/jvu5uu069imo9xc/nir%20well%20abfar.pdf?dl=0
There are two ways I know of to get high-quality images from your Chart into a PDF.
One is by using vector formats:
Use chart.SaveImage with one of the three emf formats.
Convert the resulting emf file to wmf
Insert the wmf file into your iTextSharp document.
1 and 3 are one-liners. But step 2 is not. In fact I haven't found a working c# solution at all. The best was a weird reference to an edit that has disappeared here ..
If you can use some other program to do the conversion you will get nice results like this demo pdf file.. I used Illustrator for the conversion.
Two: If you can't get step 2 to work, you can still get nice results, if you use raster images with a nice and high resolution. Here is how to do it:
First we hide the Chart, so we don't scare the user. Then we make it as large as we want the output to be. Then we DrawToBitmap and finally we reset the chart again..:
Size s = chart1.Size;
chart1.Hide();
// pick your size in pixels
// I simply multiply my screen size..:
chart1.Size = new System.Drawing.Size(s.Width * 5, s.Height * 5);
using (Bitmap bmp = new Bitmap(chart1.ClientSize.Width, chart1.ClientSize.Height))
{
// you should set the resolution,
// although I didn't find a way for iTextSharp to use it visually
bmp.SetResolution(600, 600);
using (Graphics G = Graphics.FromImage(bmp))
{
G.SmoothingMode = SmoothingMode.HighQuality;
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
bmp.Save(yourImageFile, ImageFormat.Png);
}
}
chart1.Size = s;
chart1.Show();
You could also use SaveImage and save a few lines, but you can't set the resolution of the png file there and it will be saved at the currnt screen resolution, which is 96dpi here..
Now you have a large image and will probably have to scale it down in iTextSharp:
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(yourImageFile);
img .ScalePercent(15); // scale to fit your needs..
doc.Add(img );
Note that the legend and the labels get very small this way, so you may have to enlarge them before saving. I also found that, when scaling down, the image is rather bright until you zoom in..
Here are two screenshots, one from the chart, the other from the pdf documnet after zooming in a lot (300%)..:

.NET C# - Both Bitmap and Image Loads Images Smaller than Expected

I'm trying to process the typical 16 megapixel images from a modern camera. Picking a random image, both the file system and my image editing software says the image is 4608 x 3456. When I load the image in C# (VS2013 .Net 4.5) using either new Bitmap(filename) or Image.FromFile(filename), I get an image successfully loaded. However, the resulting image has a size of 1613 x 1210. Now, in some cases I want to create custom size thumbnails, and this will work ok. However, I have another need where I detect "non normal" orientations and I simply want to flip/rotate for display, then save.
Saving these images (without any adjustments, just load and save) creates a valid image on disk. However, both the file system AND my image tool says the size is 1613 x 1210.
How do I load the full size image and preserve all info back to disk? Any ideas as to what I'm doing wrong? I just want to rotate the image where needed, I don't want to shrink it!
Here's a snippet of what I tried, as promised in a comment below:
Bitmap bm = new Bitmap(fileName);
Image jpg = Image.FromFile(fileName);
jpg.Save("e:\\test.jpg");
bm.Save("e:\\test2.jpg");
Both files are smaller than their original size, and match the width and height the debugger shows for both in-memory images.
Per a suggested answer, I tried this code but saw no difference in the results:
long width = 0;
long height = 0;
byte[] imageBytes = File.ReadAllBytes(fileName);
using (MemoryStream ms = new MemoryStream(imageBytes))
{
Image jpg3 = Image.FromStream(ms);
width = jpg3.Width;
height = jpg3.Height;
jpg3.Save("e:\\test3.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
Thanks in advance.
[EDIT]
User Error from a followup post:
While it's true I do have a 4608x3456 size image of the very picture I
am trying to load, I selected the wrong directory in my OpenFileDialog
and was actually selected a, you guessed it, 1613 x 1210 version of
this image. The code WAS loading the full thing, the silly operator
(me) was gumming it up.
Before I post, I'll try the full-size image ... yeah, it works fine.
Can you show your program code?
I downloaded 5169x3423 size sample image.
Load this image using program and when i restored it, original and new image are same.
I load image file to byte array and save it to Bitmap.
private Image LoadImage()
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.ShowDialog();
byte[] byteImage = File.ReadAllBytes(openFile.FileName);
Image myImage;
using (var ms = new MemoryStream(byteImage))
{
myImage = Image.FromStream(ms);
}
return myImage;
}
private void SaveImage(Image myImage)
{
Bitmap bmp = new Bitmap(myImage);
// Temp save location
bmp.Save("c:\\test\\myImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

Categories