Find image size with asp (vbscript) for crop script. - asp-classic

I writ a basic cropping script. In VBscript. As follows:
<%Set Jpeg = Server.CreateObject("Persits.Jpeg")
getsec = DateDiff("s","01/01/04",now())
filename = "screenshots/"&getsec&".png"
imgfile = request("imgfile")
Jpeg.Open Server.MapPath(imgfile)
Jpeg.Crop 0, 38, 320, 460
Jpeg.Canvas.DrawPNG 0, 0, Server.MapPath("watermark.png")
Jpeg.PNGOutput = True
Jpeg.Save Server.MapPath(filename)
%>
<%=filename%>
However instead of hard coding these variables:
Jpeg.Crop 0, 38, 320, 460
I would like someway of using vbscript to find out the image size before I crop it, so that if different image sizes are uploaded the script can be more flexible. Anyone know how to do this?

Your Jpeg object (ASPJpeg instance) has .OriginalWidth and .OriginalHeight properties which are accessible after invoking .Open method. You can use them.

One option is to use the loadpicture function. (see also http://www.w3schools.com/vbscript/func_loadpicture.asp)
To get the pixels, you could use this:
Dim sPath
sPath = "C:\myPicture.jpg"
dim iWidth, iHeight
dim oFs, oImg
Set oFs= CreateObject("Scripting.FileSystemObject")
Set oImg = loadpicture(sPath)
iWidth = round(oImg.width / 26.4583)
iHeight = round(oImg.height / 26.4583)
Set oImg = Nothing

Related

VB.net aspX image resize and created file permission denied

I'm trying to create an image based on an original image as a template.
a picture > kapak.jpg - resize to 1366x768 > orta.jpg - resize to 848x480
Code is working. Nothing wrong BUT when I use; code block 2 its locking the file which is kapak.jpg
I can't delete or rename etc. Even if I close the browser or vstudio or all. Nothing changes but restart the iis. I even cant delete it on the file browser as well. It's just locking it and doesnt allow anything. it says that; w3wp.exe or iis express worker is using this file so you cant delete or etc. Access denied.
I tried dispose or =nothing for bitmap
moved code block 2 to another sub
nothing changed.
If I dont use code block 2 it creates and allows me to delete or rename etc.
Any suggestions will be appreciated. How can I set free the file?
button_1_click
gelenIMG.ImageUrl = "\dene_2.jpg"
Dim uzatBOYfark As Integer
Dim uzatENfark As Integer
Dim kapakRESIMayar As New Rectangle(0, 0, 1366, 768)
uzatENfark = kapakRESIMayar.Width : uzatBOYfark = kapakRESIMayar.Height
Using orijinalRESIM = Image.FromFile(Server.MapPath(gelenIMG.ImageUrl))
If orijinalRESIM.Width < kapakRESIMayar.Width Then uzatENfark = kapakRESIMayar.Width * (kapakRESIMayar.Width / orijinalRESIM.Width)
If orijinalRESIM.Height < kapakRESIMayar.Height Then uzatBOYfark = kapakRESIMayar.Height * (kapakRESIMayar.Height / orijinalRESIM.Height)
Using kapakZEMINsablon = New Bitmap(kapakRESIMayar.Width, kapakRESIMayar.Height)
Using grp = Graphics.FromImage(kapakZEMINsablon)
grp.Clear(Color.Red)
grp.DrawImage(orijinalRESIM, New Rectangle(0, 0, uzatENfark, uzatBOYfark + 1), kapakRESIMayar, GraphicsUnit.Pixel)
End Using
kapakZEMINsablon.Save(Server.MapPath(geciciRESIMyeri & "\kapak.jpg"), Imaging.ImageFormat.Jpeg)
End Using
End Using
imgKAPAK.ImageUrl = geciciRESIMyeri & "\kapak.jpg"
code block 2:
Dim bitmapORTA As New Bitmap(Image.FromFile(Server.MapPath(imgKAPAK.ImageUrl), True), 848, 480)
bitmapORTA.Save(Server.MapPath(geciciRESIMyeri & "\orta.jpg"), Imaging.ImageFormat.Jpeg)
imgORTA.ImageUrl = geciciRESIMyeri & "\orta.jpg"

imageresizer.Plugins.Watermark on classic asp

I need to use this plugin to add watermarks to images using classic ASP. I had this work partially using this code:
dim o, b, wm, layer,textlayer
Set b = CreateObject("ImageResizer.Configuration.Config")
Set wm = CreateObject("ImageResizer.Plugins.Watermark.WatermarkPlugin")
Set textlayer = CreateObject("ImageResizer.Plugins.Watermark.TextLayer")
Set layer = CreateObject("ImageResizer.Plugins.Watermark.Layer")
textlayer.Text = "Yeees"
textlayer.fontSize = 50
layer.fill = True
set layer("dd") = textlayer 'THIS IS FAILING
wm.NamedWatermarks("sfdf") = layer("dd") 'THIS IS FAILING TOO
wm.Install(b)
b.BuildImage "C:\lg1_1361_44.jpg", "C:\lg1_1361_44_WATER.png", "watermark=tessst&format=png"
Why are you creating an instance of Layer? TextLayer and ImageLayer are the classes you want to work with
I would drop everything related to 'layer' and try this instead.
textLayer.fill = True
wm.NamedWatermarks("sfdf") = textLayer
You'll also need "watermark=sfdf" instead of "tessst"

Pdf Generation Using ITextSharp for Cheque Like Structure

I have requirement of online cheque generation.
I am using ,
using iTextSharp.text;
using iTextSharp.text.pdf;
I have dynamic cheque Height and width,font and fontsize.
And obviously cheque number with account information in all page of document.
My code goes like this
Document doc = new Document(new Rectangle(width, height), 0, 0, 0, 0);
string path = Server.MapPath("~/REPORTFILES");
var writer = PdfWriter.GetInstance(doc, new FileStream(path + "/cheque.pdf",FileMode.Create));
doc.Open();
BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
for (int i = fromchq; i <= tochq; i++)
{
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
cb.SetFontAndSize(f_cn, 14);
cb.SetFontAndSize(f_cn, fontsize);
cb.SetTextMatrix(cord_x, cord_y);
cb.ShowText(getProperty(propertyName,i.ToString().PadLeft(FromChqNo.Length, '0')));
cb.EndText();
doc.NewPage();
}
Here cord_x and cord_y is the co-ordinate location of that text.
Everything works fine i got pdf of my custom size.
like this :
But while printing it into the cheque
its work fine untill it has space enough to print single page. my xps image is attached bellow.
Area in red curve is skiped to be printed.
I mean to ask how can i make it posible that it will print serially, and completely fullfill my requirement that a cheque leaf of any height ,width,font will be printed serially. Thank you all in advance and also thanks for reading my problem.

Image upscale using system.drawing gives much larger file size than photoshop

I am trying to increase the size of an uploaded image using system.drawing. It works but the file sizes are much larger than if I were to do this using Photoshop. Is this something I will have to cope with or is there a way to decrease the file size?
I am using this code, and have tweaked the Drawing2d settings but it doesn't make an appreciable difference.
resizedImage = New Bitmap(newWidth, newHeight, image.PixelFormat)
graphics = graphics.FromImage(resizedImage)
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
graphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight)
graphics.DrawImage(image, 0, 0, newWidth, newHeight)
Do I need to use a dedicated library for this to achieve better results?
The parameters you have here are affect the "rendering" of the image, not the "image file size".
To affect the image file size, you need to drop down the quality of the jpeg when you save it, using the Image.Save Method
This is a snipped code from the MSDN Example on how you can drop down the image file size.
// Save the bitmap as a JPEG file with quality level 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
// and now save it to the file
myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);

Asp.net reduced image size in kb

I posted this other topic, got some gentle replies, but although the answers resolved the quality side of the question they created another issue with the image size in kb.
Asp.net image resizing quality
Here is the issue.
I have a 2MB image that I want to reduce to 480px width.
I used three different ways to resize it:
1) On Fileupload ran this code:
System.IO.Stream st = FileUploadPost.PostedFile.InputStream;
myImage = System.Drawing.Image.FromStream(st);
thumb = myImage.GetThumbnailImage(newWidth, newHeight, null, System.IntPtr.Zero);
thumb.Save(myPath);
Benefits: Size in kb becomes small (around 80Kb).
Downside: Visual quality is horrible
2) On Fileupload ran this code (solution provided on the mentioned post):
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
Benefits: Visual quality is good
Downside: Size continues very big (around 400kb)
3) Used Windows Paint software and "manually" reduced the image to 480px width
Benefits: Benefits of both 1) and 2) -> Visual quality is good and size is reduced to around 80kb
Question is:
What is the code that reproduces item 3 behaviour (good visual quality and smaller size in kb)?
Thanks
Try one of these:
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
or
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
or
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
This should only slightly degrade the quality and drastically reduce the size, Im not sure which one would be better though.
Here are a whole bunch of other options to try as well:
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat%28v=vs.110%29.aspx
protected void Page_Load(object sender, EventArgs e)
{
//BR**
string filePath = "";//Source file path with image name
int CompressLevel = 50;//Image compression leve as per our requirement
string DestintionPath = "";//Destination file path
string Filename = "";//Output image name to save in destination path
Stream bmpStream = System.IO.File.Open(filePath, System.IO.FileMode.Open);
Bitmap bmp1 = new Bitmap(bmpStream);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, CompressLevel);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(DestintionPath + "\\" + Filename, jpgEncoder, myEncoderParameters);
//BR**
bmpStream.Close();
string lblmsg = "Compressed Sucessfully with Compression Level { " + CompressLevel.ToString() + " }";
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
/*/ Hope this will work.Thanks in advance /*/
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}

Resources