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.
Related
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+/8AAEQgAFABHAwER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LuZLdkS4jt9Q+Avj60meBpI5o1mWOZmiaSKVA4BeN1BSgD039jHwjpHgD9j79lHwH4fWVNB8E/s1/AvwjoiTmAzppHhv4X+FtG01ZjawWtsZVs7KESG3treDfnyYIYysagBD+yX8FI/wBsHUP2630LVbj9ou9/Zr0f9ku28TTeIdWGiaR8FNM+KGu/GG90LT/CkFxBoUmq+IvHWr6fqGseIdVtNT1aG08L6Fpvh+40Kzn8TR+IgD6UoA5n/hCvBw8ZH4ijwn4aHxBbwyvgpvHI0PTB4wbwcmqtrqeE28S/Zf7Zbw0mtu+sJoZvP7MXVHe/FqLt2loA6agD54/Z4/ZT+Av7Kdt8YrT4DeB5/BMXx++PHxB/ab+Lr3XjDx14zufGXxy+KkeiR+PvHdxe+PfE3ii80ufxCvh3RzNoegz6X4XsZLRpdL0Sxlurx7gA5/8Aa6/Yn/Zd/bx+HGhfCL9rX4S6P8Zvhv4b8c6b8SdH8K63qviXSLO08a6R4f8AE/hfTdcW68K61oOptPaaH4x8R2SQtfG1dNRkaWB3SNlAP//Z")))
{
Image tmpimage = Image.FromStream(ms);
tmpimage.RotateFlip(RotateFlipType.RotateNoneFlipY);
tmpimage.Save(ms, ImageFormat.Png);
var image = page.Content.AddImage(new ImageSource(ms));
}
Thanks for any help!!
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
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);
}
Right now I have a form with a PictureBox on the form. I'm using two partially transparent images and trying to put one on top of the other.
sideMenuWide = rounded rectangle with transparent background (.png) [Bottom image]
labelPointer = triangle with transparent background (.png) [Top image]
Here is my approach:
// METHOD #1 //
Image img1 = Image.FromFile(#"C:\sideMenuWide.png");
Image img2 = Image.FromFile(#"C:\labelPointer.png");
picBox.Image = CombineImages(img1, img2);
// METHOD #2 //
Image imgA = RBS.Properties.Resources.sideMenuWide;
Image imgB = RBS.Properties.Resources.labelPointer;
picBox.Image = CombineImages(imgA, imgB);
And the CombineImage function: (I did not write this function, only modified)
public static Bitmap CombineImages(Image imgA, Image imgB)
{
//a holder for the result (By default, use the first image as the main size)
Bitmap result = new Bitmap(imgA.Size.Width, imgA.Size.Height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the images into the target bitmap
graphics.DrawImage(imgA, 0, 0, imgA.Width, imgA.Height);
graphics.DrawImage(imgB, 100, 70, imgB.Width, imgB.Height);
}
return result;
}
Method #1 DOES work how I want it to, displays imgB perfectly on top of imgA.
Method #2 however shows imgA just fine but imgB is really faint.
Any ideas on how to overcome this? I would like to be able to do this using Resources instead of having to pull from a file.
If the files work when loaded, but the resources don't, then it sounds like the resx build process or the ResourceManager is doing something undesirable. I'd try embedding the files and reading them directly from a stream rather than relying on the ResourceManager to do it for you.
In your Solution Explorer, Add Existing File to add the file to your project. Get properties on the added file and set it to Embedded Resource=. (Don't add it to a resx file)
In your code, you can now get a stream containing the file's data:
Stream instream = Assembly.GetExecutingAssembly().
GetManifestResourceStream("RBS.labelPointer.png"));
(Hint: The compiler generates an obtuse name for the embedded resource, so after adding the files you can add temporary code to call Assembly.GetExecutingAssembly().GetManifestResourceNames() to get a list of them all and find the file you're interested in)
Load your bitmap/image from the stream:
Image img2 = new Bitmap(inStream);
I have a problem adding cliparts (from png files) to bmp object. I add some cliparts to a jpeg image and after I save it, I get a mistery: some cliparts are present in saved image, but some not.
Here is code how I add cliparts to image:
using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(this._image))
{
gfx.CompositingMode = CompositingMode.SourceOver;
gfx.CompositingQuality = CompositingQuality.HighQuality;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(image, posX, posY, newWidth, newHeight);
gfx.Save();
}
Can anybode explain what am I doing wrong or why this situation appears?
edited:
it happens when I use this code inside a ASP.NET Application, when I use it in windows application everything is fine
I'm not sure if this is the cause of Your issue, but line gfx.Save() does not save the image you painted back to the image, but saves state of the Graphics object. In order to save the changes back to the image, make sure to call Image.Save() method and double check to make sure You are not restoring the previous image somewhere in the code. More information about it Graphics.Save method can be found here: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.save(v=vs.100).aspx.
Update
You wrote in the comments that it's a transparency issue. If this is the cause, take a look at the code loading the clipart images from files. This also may be caused by various pixel formats used in your image files. Changing You images loading algorithm to something like this may solve the issue:
load your background image, let's say it's done like this: Image backgroundImg = new Bitmap(backgroundImgPath);
create a new empty image as a base for all the images: this._image = new Bimap(backgroundImg.Width, backgroundImg.Height, PixelFormat.Format32bppArgb);
draw the contents of backgroundImg onto this._image with gfx.CompositingMode = CompositingMode.SourceCopy;
then, you can draw the cliparts as You're doing it now (just comment out gfx.Save(); method)
Let me know if this helped.