I'm having a problem when fetching an image using Image.GetInstance. The photo is in portrait but when I did the Image.GetInstance(), my image is rotated.
string imagepath = AttachmentURL + answer.Attachment.AzureFileName;
Image image = Image.GetInstance(imagepath);
My image:
https://zensyd.blob.core.windows.net/fileattachments/f9ab6a11-13c1-421a-abdd-4b4a6d701033.jpg
This means 2016 width and 1512 height.
I found this answer regarding image orientation rotation via exif but I need to convert the uri first to a memory stream. I then found this solution that converts URI to a MemoryStream. Then MemoryStream to System.Drawing Image.
Here's my solution
BaseColor color = null;
string imagepath = AttachmentURL + answer.Attachment.AzureFileName;
var imgStream = GetStreamFromUrl(imagepath);
var tempImg = System.Drawing.Image.FromStream(imgStream);
ExifRotate(tempImg);
Image image = Image.GetInstance(tempImg, color);
GetStreamFromUrl is balexandre's answer here
ExifRotate is saucecontrol's answer here.
Related
I need help for converting Emgucv videoCapture image to LoadRawTextureData of unity to display images/videos in Unity3d 2018.1.
I am able to display images but it show strange lines and distorted image effects or some kind of shuttering/scattering.
I read the question in this post and apply the solution but the problem is not solved Convert Mat to Texture2d(Stackoverflow)
I think the Colorspace of Emgucv image did not matched with the texture2d.
Code:
void Start () {
vc = new VideoCapture(0);
vc.FlipVertical = true; //The image I am getting from webcam is flipped
myMaterial = GetComponent<Renderer>().material;
frameHeight = (int)vc.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight);
frameWidth = (int)vc.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth);
camera = new Texture2D(frameHeight,frameWidth, TextureFormat.RGB24, false,false);
}
I am getting images in this part of code and converting the color space as per texture2d format.
void Update () {
Mat secondimage = new Mat();
Mat myimage = vc.QueryFrame();
CvInvoke.CvtColor(myimage, secondimage, Emgu.CV.CvEnum.ColorConversion.Bgr2Rgb);
Image<Rgb, byte> hello = secondimage.ToImage<Rgb, byte>();
camera.LoadRawTextureData(hello.bytes);
camera.Apply();
myMaterial.mainTexture = camera;
}
Results:
The Result is kind of strange,take a look on following result images.
This is my hand holding in Front of camera.This image is orignal texture2d image.
Image1
This image is displayed on the 3d plane in unity3d.
Image2
As code explain i already Convert image Bgr2Rgb.
update:
I simply exchange the width and height in texture2d and wow it worked.
camera = new Texture2D(frameWidth, frameHeight, TextureFormat.RGB24,false,false);
How can I add a png image as a watermark to a larger image using Xamarin.iOS c# and save the output to the device?
I figured out the Xamarin.Android version from another question posted here.
Thanks in Advance!!
Using an image context, you can draw the original, then the watermark at the necessary location and obtain a new image from the context.
ImageContext example:
var originalImage = UIImage.FromBundle("buymore.jpg");
var watermarkImage = UIImage.FromFile("vs.png");
UIGraphics.BeginImageContextWithOptions(originalImage.Size, true, 1.0f);
originalImage.Draw(CGPoint.Empty);
watermarkImage.Draw(new CGRect(new CGPoint(200, 200), watermarkImage.Size));
var processedImage = UIGraphics.GetImageFromCurrentImageContext();
If your original and watermark images are the same size, you can use a CIFilter (CISourceOverCompositing) to "overlay" one image on top of another (assuming your watermark has a white or alpha background. This is my preferred method due to the speed.
CISourceOverCompositing example:
UIImage processedimage;
using (var filter = new CISourceOverCompositing())
{
filter.Image = new CIImage(UIImage.FromBundle("vs.png"));
filter.BackgroundImage = new CIImage(UIImage.FromBundle("buymore.jpg"));
processedimage = UIImage.FromImage(filter.OutputImage);
}
I am trying to rotate the image based on EXIF tag. I am able to handle the rotation of image successfully but the thumbnail in windows explorer is still upside down. The image when opened is absolutely fine. Verified the corrected orientation here. The problem with the following code is that the EXIF data does not seem to have any information about thumbnail orientation. What I want is:
If there is thumbnail orientation available, rotate the thumbnail and update image's metadata for thumbnail orientation.
If there is no thumbnail orientation information available, rotate the thumbnail and add image's metadata for thumbnail orientation.
The code I am using is:
public static RotateFlipType RotateImageByExifOrientationData(Image img, string oldFileName, string sourceFilePath, out string newFileName)
{
int orientationId = 0x0112;//Image orientation
int thumbnailOrientationId = 0x5029;//Thumbnail orientation
var fType = RotateFlipType.RotateNoneFlipNone;
if (img.PropertyIdList.Contains(orientationId))
{
var pItem = img.GetPropertyItem(orientationId);
//Get the orientation
fType = GetRotateFlipTypeByExifOrientationData(pItem.Value[0]);
if (fType != RotateFlipType.RotateNoneFlipNone)
{
img.RotateFlip(fType);
// Read orientation tag. Update to normal so that the other clients(image viewer or browser) will not rotate the rotated image.
// Force value to 1
pItem.Value = BitConverter.GetBytes((short)1);
img.SetPropertyItem(pItem);
PropertyItem thumbnailItem;
if (img.PropertyIdList.Contains(thumbnailOrientationId))
{
//If thumbnail metadata is available, update it.
thumbnailItem = img.GetPropertyItem(thumbnailOrientationId);
thumbnailItem.Value = BitConverter.GetBytes((short)1);
img.SetPropertyItem(thumbnailItem);
}
else
{
//If thumbnail metadata is not available, add appropriate metadata.
thumbnailItem = img.PropertyItems[0];
thumbnailItem.Id = thumbnailOrientationId;
thumbnailItem.Type = 2;
thumbnailItem.Value = BitConverter.GetBytes((short)1);
thumbnailItem.Len = thumbnailItem.Value.Length;
img.SetPropertyItem(thumbnailItem);
}
newFileName = "Rotated_" + oldFileName;
string targetFilePath = sourceFilePath + newFileName ;
ImageFormat targetFormat = ImageFormat.Jpeg;
img.Save(targetFilePath, targetFormat);
File.Delete(sourceFilePath + oldFileName);//Delete old file.
}
}
return fType;
}
is an old question, but after some research, I noticed that even after removing the orientation of the Image (0x0112) and the orientation of the thumbnail (0x5029), when trying to generate the thumbnail again, the same orientation remained. So I checked that some JPGs have the thumbnail "embedded" in bytes. so after removing the bytes (0x501B), I was able to generate the thumbnail correctly.
Simple code shows:
var rotateImage = Image.FromStream(fileStream);
switch (degree)
{
case eRotateImagem.Degree_90:
rotateImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case eRotateImagem.Degree_180:
rotateImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case eRotateImagem.Degree_270:
rotateImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
}
int orientationId = 0x0112; //Image orientation
int thumbnailOrientationId = 0x5029; //Thumbnail orientation
int thumbnailBytes = 0x501B; //Thumbnail bytes
if (rotateImage.PropertyIdList.Contains(orientationId))
{
rotateImage.RemovePropertyItem(orientationId);
}
if (rotateImage.PropertyIdList.Contains(thumbnailOrientationId))
{
rotateImage.RemovePropertyItem(thumbnailOrientationId);
}
if (rotateImage.PropertyIdList.Contains(thumbnailBytes))
{
rotateImage.RemovePropertyItem(thumbnailBytes);
}
I upload a file , after upload base on info i got , i resize image , crop and resize it again
but just first resize work , crop and second resize do noting on image, after first resize what i must to do ?
WebImage img = new WebImage(file.InputStream);
img.Resize(image_sw, image_sh);
img.Crop(image_y, image_x, image_y + image_h, image_x + image_w);
img.Resize(300, 300);
var path = Request.MapPath("~/Content/images/category/" + model.ID + "." + type);
img.Save(path);
The Resize method has a return argument with the new resized image. Just saving the img will save the old image in the old sizes.
Try this:
img = img.Resize(300, 300);
I have two images and I want to convert it to one pdf file with one image in a page...
what is the easiest way of doing it in C#?
Edit: I tried the following code (added the PdfSharp reference):
string source1 = #"MySource1.JPG";
string source2 = #"MySource2.JPG";
string destinaton = #"MyDest.pdf";
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source1);
XGraphics xgr2 = XGraphics.FromPdfPage(doc.Pages[1]);
XImage img2 = XImage.FromFile(source2);
xgr.DrawImage(img, 0, 0);
xgr2.DrawImage(img2, 0, 0);
doc.Save(destinaton);
doc.Close();
Now it is create a pdf with my two pages but the problem now is that the images are cut and not in their original size! the size of the pictures is 3264x2448.
How can I fix the image size to the pdf document size?
There are several overloads of DrawImage. Use an overload that allows you to specify the destination size of the image.
Three lines of code allow to calculate the image size to use the complete page (with a margin if wanted) while keeping the aspect ratio.
If you know the pdf document size, you can resize your image as:
Bitmap objBitmap = new Bitmap(objImage, new Size(size1, size2));
where objImage is your original image.
or like:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
objBitmap = resizeImage(objBitmap, new Size(size1,size2));