how to reduce Image width and height in c# asp.net? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on a project where have to display the images by using image path.
For this I write one method taking the image path as an argument.
My goal is to reduce the size of image(width and height)

If you pass the image into an instance of the WebImage Class you can do those things simply using the Resize method.
var webImage = new WebImage(image);
webImage.Resize(200, 200, false, true);
webImage.Save("~/path", "png", true);
In the example, image passed into the WebImage as a param could be a byte[] for the file or just a string path to the file. I set the image dimensions to 200*200 and saved it as a PNG.

What you're looking for is image resampling.
Here's a VERY quick and dirty way of resampling an image in C#.NET using Bilinear interpolation.
Bitmap bmpOriginal = Bitmap.FromFile("path_to_file");
Bitmap bmpResampled = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(bmpResampled);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.DrawImage(bmpOriginal, new Rectangle(0, 0, bmpResampled.Width + 1, bmpResampled.Height + 1));
Your resampled version will now be contained in bmpResampled.

If you just want to edit width and height without any complications and changing the actual size.
This is pure html way.
<img id="" src ="Your image path" height ="100" width = "100"></img>
or you can use an asp control
<asp:Image ID ="img" runat ="server" ImageUrl = "your path" Width ="100" Height ="100"/>
that is if your problem is this simple. Just put height and width values accordingly.

Related

Possibility of showing System.Drawing.Graphics object in a WPF control [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 28 days ago.
Improve this question
I'm using PrintDocument to print a receipt in for a POS system. I'm using the PrintPage event handler's graphics object to do the printing. The application is written using WPF with .NET 7.
It would be good if I could show a preview in the WPF application before printing. Is there any possibility to display a System.Drawing.Graphics object in a user control? If I could, I can re-use the same logic.
You cannot display a System.Drawing.Graphics object directly. What you can do is draw to a bitmap with Graphics.FromImage and display the bitmap in wpf.
Something like:
var bitmap = new Bitmap(512, 512);
using(var g = Graphics.FromImage(bitmap)){
// Do drawing
}
var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(), // you will need to delete this hbitmap
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(512, 512));

Is there a faster way to get the dimensions of a jpg image from a file than using System.Drawing.Image.FromFile [duplicate]

This question already has answers here:
Getting image dimensions without reading the entire file
(9 answers)
Closed 7 years ago.
Width and Height of an image inside a jpg file can be retrieved by reading all file data into an Image.
System.Drawing.Image img = System.Drawing.Image.FromFile("a.jpg");
var w = img.Width
var h = img.Height
This is costly however and if only width and height are needed, there might be a better way.
So I wonder if there is a less resource intensive way to get the image dimensions. In my (common) scenario, the jpg is uploaded to the web server and the web application shall just get width and height and store that. I thought, the jpg file format might have width and height as easy readable metadata and if so, someone knows how to access it.
You may want to parse the file header directly instead of constructing an image, the width and height are in there
http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format#File_format_structure

how to smooth the edge of the image from disk/exporting to pdf(c#)

im using itextsharp for exporting the image to pdf.
---- i want to make the edges of image smooth (curved edges),
---- and by itextsharp image property to get the image width and height(while getting the image from disk)
---- and also how to set the background color of the pdf page
following is for getting image and adding to pdf:
pdfDoc.Open();
//pdfDoc.Add(new iTextSharp.text.Paragraph("Welcome to dotnetfox"));
iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(#"C:\Users\Admin\Desktop\logoall.bmp");
// gif.ScaleToFit(500, 100);
pdfDoc.Add(gif);
following is making grid to image and saving to disk:
Grid companysnapshot = values[0] as Grid; //companysnap shot
companysnapshot.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
int companywidth = (int)Math.Round(companysnapshot.ActualWidth);
int companyheight = (int)Math.Round(companysnapshot.ActualHeight);
companywidth = companywidth == 0 ? 1 : companywidth;
companyheight = companyheight == 0 ? 1 : companyheight;
RenderTargetBitmap rtbmp = new RenderTargetBitmap(companywidth, companyheight, 96d, 96d, PixelFormats.Default);
rtbmp.Render(companysnapshot);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtbmp));
FileStream fs1 = File.Create(#"C:\Users\Admin\Desktop\companyss.bmp");
encoder.Save(fs1);
fs1.Close();
please code me out for this!!
You have combined multiple question into one post. That's not the way you should post questions.
Anyway:
Question 1: What is the size of an image?
You have an Image instance gif. The witdh of this image is gif.ScaledWidthand jpg.ScaledHeight. There are other ways to get the width and the height, but this way always gives you the size in user units that will be used in the PDF.
If you do not scale the image, ScaledWidth and ScaledHeight will give you the original size of the image in pixels. Pixels will be treated as user units by iText. In PDF, a user unit corresponds with a point by default (and 72 points correspond with 1 inch).
Question 2: How do you display the image with rounded corners?
Some image formats (such as PNG) allow transparency. You could create an image in such a way that the effect of rounded corners is mimicked by making the corners transparent.
If this is not an option, you should apply a clipping path. This is demonstrated in the ClippingPath example in chapter 10 of my book.
Ported to C#, the example would be something like this:
Image img = Image.GetInstance(some_path_to_an_image);
float w = img.ScaledWidth;
float h = img.ScaledHeight;
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h);
t.Ellipse(0, 0, w, h);
t.Clip();
t.NewPath();
t.AddImage(img, w, 0, 0, h, 0, -600);
Image clipped = Image.GetInstance(t);
Of course: this clips the image into an ellipse as shown in the resulting PDF. You need to replace the Ellipse() method from the example with the RoundRectangle() method.
Question3: How to give each page a background color?
This is a duplicate question. Please read the answers to the following questions:
add header, footer and Background color to pdf cells in iTextSharp
How to draw border for whole pdf pages using iText library 5.5.2
Why this pageEvent cover the content of page using iText library?
Adding a background color is done using page events and you'll find the code on how to do this in the questions mentioned above.

Getting the resolution of an image [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get the resolution of a jpeg image using C# and the .NET Environment?
In a batch image downloader I am coding, I use a WebClient (DownloadFile) to save images from given urls. Is there an easy way to get the resolution of these images? If the WebClient cannot, how can i get the resolution after the file is saved?
To get the DPI use the following:
Image image = Image.FromFile("image.jpg");
image.HorizontalResolution;
For other things such as height, width, and size, view this stackoverflow question which has many good answers, (including mine :D ).
If you save the image after downloading it from the WebClient, you can use the following:
Image img = Image.FromFile(#"image.png");
Console.WriteLine(img.Width + "x" + img.Height);
This will give you the width x height of the image, for example, 1920x1080.

Disabling antialiasing on a WPF image [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm writing a small Login dialog, and have embedded a banner at the top of the dialog for aesthetic reasons. All went well, except that by default, WPF anti aliases the entire image, making the text that was contained within it frustrating blurry.
After a bit of searching, the first few pages of results showed that it's common belief that anti aliasing cannot be disable in WPF. Can any confirm, or otherwise deny this?
It's a minor issue for me - I'll take the text out of the image and instead superimpose a label with the same text on top of the background image to achieve the same effect (though I must admit, it's a bit annoying).
Thanks,
Rob
As far as I know, WPF always does anti-aliasing when scaling a bitmap. However you should be able to accomplish your goal by avoiding the bitmap scaling.
There are two steps:
Set SnapsToDevicePixels="true" on your image
Set a ScaleTransform on your image to scale it so that one device pixel = one bitmap pixel
To compute the needed ScaleTransform, compute your screen's DPI like this:
var DPI = Win32Functions.GetSystemMetrics(SM_CYICON) / SystemParameters.IconHeight * 96;
and then for the bitmap, do:
var scale = bitmapDPI / DPI;
var transform = new ScaleTransform(scale, scale);
This will cause your bitmap's pixels to exactly match with the device pixels. WPF will not stretch the bitmap, so there should be no anti-aliasing.
If you do want to stretch your image on high DPI screens but do so without anti-aliasing (eg double all pixels), just stretch the bitmap in your own code using whichever algorithm you like and use the above with the stretched bitmap.
It's not really anti-aliasing - it's sub pixel positioning that causing the problem, I've written about it (and about a control that solves the problem) on my blog at:
http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx

Categories

Resources