You can reduce the quality of photos to speed up the loading of your site
If you own a website or work on a customer website, then you need to know the importance of SEO. Without proper SEO strategies, you can not get high rankings in Google.
JavaScript is a complex language. If you are a JavaScript developer at any level, it is important to understand the basics. This article addresses 12 concepts that are important for any JS developer to understand, but by no means cover the full extent of what a JS developer needs to know.
Success in a media channel such as organic search depends on the content. In particular, the production of useful content has the ability to rank. In its recent algorithm updates, Google has focused heavily on promoting great content and natural links, and penalizing poor content with unspecified links.
Websites are created using different frameworks and programming languages. The .NET framework is one of the many options you can choose from.
Reducing the size of the image is important for the speed of the site because the loading speed of the site increases
Today I will teach you how to increase the size of the site
While I was creating this site, I was researching on the same subject when I came across a link that wrote the code.
private void VaryQualityLevel()
{
// Get a bitmap. The using statement ensures objects
// are automatically disposed from memory after use.
using (Bitmap bmp1 = new Bitmap(@"C:\TestPhoto.jpg"))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"C:\TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters);
// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"C:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters);
}
}
Please use these codes
using System.Drawing; using System.Drawing.Imaging; |
This code has different parts, but I will explain the important part
For example:
This code will take the desired photo from you
using (Bitmap bmp1 = new Bitmap(@"C:\TestPhoto.jpg"))
And the important part of this part is creating the volume of the new photo
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);
That number at the end of the first line adjusts the quality and can be adjusted from 0 to 100 (50L)
Well if you use this code in Asp.Net MVC it will probably give an error
And now my suggestion
My suggestion is to change this code a bit
var GuidRan = Guid.NewGuid();
var Format = Path.GetExtension(imgUp.FileName);
page.ImageName = GuidRan + Path.GetExtension(imgUp.FileName);
imgUp.SaveAs(Server.MapPath("/PageImages/" + page.ImageName));
FileInfo imgInfo = new FileInfo(Server.MapPath("/PageImages/" + page.ImageName));
if (imgInfo.Length > 1000000)
{
using (Bitmap bmp1 = new Bitmap(Server.MapPath("/PageImages/" + page.ImageName)))
{
System.Drawing.Imaging.ImageCodecInfo GetEncoder(System.Drawing.Imaging.ImageFormat format)
{
var codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders();
foreach (var codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
var jpgEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(Server.MapPath(@"/PageImages/" + GuidRan), jpgEncoder, myEncoderParameters);
}
System.IO.File.Delete(Server.MapPath("/PageImages/" + page.ImageName));
System.IO.File.Copy(Server.MapPath("/PageImages/") + GuidRan,
Server.MapPath("/PageImages/") + GuidRan + Format, true);
System.IO.File.Delete(Server.MapPath("/PageImages/") + GuidRan);
}
Use this here as well
using System.IO; |
This is the first code that has changed a bit
Well, let's go to the changes that have been made to explain to you
Here I used Guid to make the photos look the same and I used a variable called GuidRan
Then I had to get an image format and keep it in a variable with an error
var Format = Path.GetExtension(imgUp.FileName); |
Then I save the photo
And in the next line, I give the address of the saved file to the volume reduction code
Inside the section, you will notice another change that is in the addressing location
Server.MapPath |
This goes to the project location
Tip!
While testing the project, I noticed that if the image is below 1 MG, the image looks like the image below
This photo is a total of 261KB and after reducing the size of the photo, it looks like this
The rest of the parts are the same as before, except for the last code you remember that I pointed to an error that pointed to the format. Well, I solved this error with a little creativity.
Here we have the steps of taking photos and saving ...
To tell you dear ones with a number
The file extension is not entered when saving the second image
After the System.IO we get help
First we delete the photo we took from the user (quality photo)
After the photo, copy the extension and convert it to a photo with the extension
Then we delete the photo without the extension
Just as easily
Friends, I hope you enjoyed this article
Please support us to remove the word Beta from the name of our site
Thanks to Microsoft🙏
This post is one of the best posts on the site, thank you admin