jpeg not loading with Response.Outputstream on server - c#

i'm working on an ASP.net 2.0 c# project where i am creating a basic Captcha script. the html looks like this:
<img height="30" width="80" alt="" src="Captcha.aspx" />
and here is the code behind for Captcha.aspx
protected void Page_Load(object sender, EventArgs e)
{
Bitmap objBMP = new System.Drawing.Bitmap(60, 20);
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
objGraphics.Clear( ColorTranslator.FromHtml( "#054196" ) );
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
// configure the text
Font objFont = new Font("Arial", 8, FontStyle.Bold);
string randomStr = "";
int[] myIntArray = new int[5];
int x;
// randomise the text
Random autoRand = new Random();
for (x = 0; x < 5; x++)
{
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
randomStr += (myIntArray[x].ToString());
}
//add string to session
Session.Add("randomStr", randomStr);
// draw the text
objGraphics.DrawString( randomStr, objFont, Brushes.White, 3, 3);
// Set the content type and return the image
Response.ContentType = "image/jpeg";
Encoder quality = Encoder.Quality;
EncoderParameter qualityParam = new EncoderParameter(quality, 100L);
EncoderParameters encParams = new EncoderParameters( 1 );
encParams.Param[0] = qualityParam;
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
objBMP.Save(Response.OutputStream, jpgEncoder, encParams);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
Response.Flush();
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
this works fine on my local machine, but when uploaded to our dev server it fails. i don't have direct access to the dev server to debug due to my role in the project so it's a bit of trial and error atm.
any ideas?

turns out the server didn't like the aspx page for some reason. so i moved the captcha to a ashx file and then it worked!

Related

Getting exception when trying to save an image that was edited locally

I'm trying to edit an image by including a water mark on it. I make my changes using Graphics and when I try to save the file it throws me an exception Message:"A generic error occurred in GDI+."
Below is my code:
public static void Test()
{
try
{
Bitmap bmpPic = new Bitmap(Image.FromFile("Desktop/cropped/cropped/croppedsent1386.jpeg"));
using (Graphics g = Graphics.FromImage(bmpPic))
{
Brush brush = new SolidBrush(Color.FromArgb(80, 255, 255, 255));
Point postionWaterMark = new Point((bmpPic.Width / 6), (bmpPic.Height / 2));
g.DrawString("Identifid", new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel), brush, postionWaterMark);
}
string filepath = "Desktop/cropped/cropped/croppedsent1386.jpeg";
bmpPic.Save(filepath, ImageFormat.Jpeg);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("End of test ");
}
-Stack Trace-
StackTrace " at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)\r\n at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ConsoleWatermark.Program.Test() in C:\\Users\\xavie\\source\\repos\\Samples\\ConsoleWatermark\\Program.cs:line 49"
I'm stumped as to why this is failing as I have permissions to edit files .
Error appears because you trying to overwrite (when bmpPic.Save()) same image file, which you already loaded as Bitmap. Until Bitmap wouldn't be disposed - file would be locked by it. To solve, you should save new watermarked image to an other new file:
string filepath = ".../croppedsent1386_WATERMARKED.jpeg"; // <--- New file
bmpPic.Save(filepath, ImageFormat.Jpeg);
Complete version (with few notations):
static void Main(string[] args)
{
try
{
// Source image file
string imageFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "myImage.png");
// Add "using" to bitmap too for proper disposage and to release file after completion
using (Bitmap bitmap = new Bitmap(Image.FromFile(imageFile)))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
Brush brush = new SolidBrush(Color.FromArgb(80, 255, 255, 255));
Point watermarkPosition = new Point(bitmap.Width / 6, bitmap.Height / 2);
g.DrawString("IDENTIFID", new Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel), brush, watermarkPosition);
// Save to a new file
string newImageFile = imageFile.Replace("myImage.png", "myImage_WATERMARKED.png");
bitmap.Save(newImageFile, ImageFormat.Png);
}
}
Console.WriteLine("Image saved!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\n\nStack trace:\n" + ex.StackTrace);
}
Console.ReadKey();
}

Read text from screenshot

I'm using selenium to go to x website and take screenshot
public static void TakeScreenshot(IWebDriver driver, int x, int y, int width, int height)
{
var name =
$#"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\{"screenshots"}\{Guid.NewGuid()}.{
ScreenshotImageFormat.Png
}";
Rectangle rect = new Rectangle(x, y, width, height);
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
var bitmapScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
var croppedArea = new Rectangle(rect.Location, rect.Size);
bitmapScreen.Clone(croppedArea, bitmapScreen.PixelFormat).Save(name);
}
and after saving that I'm trying to read text from that image using tesseract
var testImagePath = #".\Content\300.png";
var dataPath = #".\tessdata";
try
{
using (var tEngine = new TesseractEngine(dataPath, "eng", EngineMode.Default)) //creating the tesseract OCR engine with English as the language
{
using (var img = Pix.LoadFromFile(testImagePath)) // Load of the image file from the Pix object which is a wrapper for Leptonica PIX structure
{
using (var page = tEngine.Process(img)) //process the specified image
{
var text = page.GetText(); //Gets the image's content as plain text.
Console.WriteLine(text); //display the text
Console.WriteLine(page.GetMeanConfidence()); //Get's the mean confidence that as a percentage of the recognized text.
Console.ReadLine();
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Unexpected Error: " + e.Message);
}
but I'm getting that dummy text:
so I tried to rescale that image
bitmapScreen.SetResolution(300, 300);
as I found here
but result is the same

I have to compress the PNG file image, without losing the quality

I want to compress a PNG image, to reduce its size but the quality should remain the same. I have tried to compress JPEG picture. Picture compressed about 90% and quality remain the same but when i compress a PNG image with it. No result, no compression. Same size.
Here is my code.
public const string _StatusLog = "StatusLog.csv";
static void Main(string[] args)
{
Console.WriteLine(" ### WELCOME ###");
Console.Write("\n\nPlease enter image folder path :");
string imagePath = Console.ReadLine();
Program p = new Program();
p.VaryQualityLevel(imagePath);
Console.ReadLine();
}
private void VaryQualityLevel(string pathOfImage)
{
try
{
//Console.Write("Target Directory Path :");
string targetDirectory = pathOfImage;//Console.ReadLine();
if (targetDirectory != null)
{
string[] allDirectoryInTargetDirectory = Directory.GetDirectories(targetDirectory);
//PRODUCT DIRECOTY OPEN
Console.Write("Total Folders found = " + allDirectoryInTargetDirectory.Count());
Console.Read();
if (allDirectoryInTargetDirectory.Any())
{
foreach (var directory in allDirectoryInTargetDirectory)
{
string[] subDirectory = Directory.GetDirectories(directory); // ATTRIBUTE DIRECTORY OPEN
if (subDirectory.Any())
{
foreach (var filesInSubDir in subDirectory)
{
string[] allFilesInSubDir = Directory.GetFiles(filesInSubDir);
//FILES IN SUB DIR OPEN
if (allFilesInSubDir.Any())
{
foreach (var imageFile in allFilesInSubDir)
{
try
{
Bitmap bmp1 = new Bitmap(imageFile);//pathOfImage);
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.
#region SAVING THE COMPRESS IMAGE FILE
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(filesInSubDir + "\\" + "Zip" + GettingImageNameForOptimizedImage(imageFile), jpgEncoder, myEncoderParameters);//pathOfImage
Console.WriteLine(filesInSubDir + GettingImageNameForOptimizedImage(imageFile) + " CREATED");//pathOfImage
#endregion
#region DELETING THE ORIGNAL FILE
bmp1.Dispose();
System.IO.File.Delete(filesInSubDir + "\\" + GettingImageNameForOptimizedImage(imageFile));//pathOfImage
Console.WriteLine(imageFile.Replace("jpg", "png") + " DELETED");//pathOfImage
#endregion
//myEncoderParameter = new EncoderParameter(myEncoder, 100L);
//myEncoderParameters.Param[0] = myEncoderParameter;
//bmp1.Save("D:\\" + RemovingImageFormat[0] + "100L" + ".jpg", jpgEncoder, myEncoderParameters);
#region BACK RENAMING FILE TO ORIGNAL NAME
System.IO.File.Move(filesInSubDir + "\\" + "Zip" + GettingImageNameForOptimizedImage(imageFile), filesInSubDir + "\\" + GettingImageNameForOptimizedImage(imageFile));
#endregion
}
catch (Exception ex)
{
Console.Write("\n" + ex.Message + " Press enter to continue :");
Console.ReadLine();
Console.Write("\nWould you like to retry ? [Y/N] :");
string resp = Console.ReadLine();
if (resp == "Y" || resp == "y")
{
Console.WriteLine(" -------------------\n\n");
Main(null);
}
}
}
}
}
}
}
}
}
}
catch (Exception ex)
{
Console.Write(ex);
Console.Read();
}
Console.Write("Press any key to exit...");
Console.Read();
// Get a bitmap. ###################################################################
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
public string GettingImageNameForOptimizedImage(string pathOfImage)
{
try
{
string[] splitingPathOfImage = pathOfImage.Split('\\');
string[] RemovingImageFormat = splitingPathOfImage[splitingPathOfImage.Count() - 1].ToString().Split('.');
return RemovingImageFormat[0] + ".jpg";
}
catch (Exception)
{
return null;
}
return null;
}
public static void LoggingOperations(string ImageName, string Status, bool UpdateRequired)
{
try
{
if (!File.Exists(_StatusLog))
{
using (File.Create(_StatusLog)) { }
DirectorySecurity sec = Directory.GetAccessControl(_StatusLog);
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(_StatusLog, sec);
}
if (UpdateRequired == true)
{
string UpdateStatusText = File.ReadAllText(_StatusLog);
UpdateStatusText = UpdateStatusText.Replace(ImageName, ImageName + "," + Status);
File.WriteAllText(_StatusLog, UpdateStatusText);
UpdateStatusText = "";
}
else
{
File.AppendAllText(_StatusLog, Environment.NewLine);
File.AppendAllText(_StatusLog, Status);
}
}
catch (Exception)
{
}
}
For PNG compression i changed the following line.
Bitmap bmp1 = new Bitmap(imageFile);//pathOfImage);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Png);
Kindly some one help me out. If there is a new method, I welcome it. If this could be altered, it would a lot better.
PNG Images are 32 bits by default. You can convert them to 8 bits : resulting file will be about 5 times smaller than the original one. With most images, the loss of quality is almost invisible.
This is what online png compressors do.
You can do this yourself by using nQuant : http://nquant.codeplex.com/ (available on Nuget)
var quantizer = new WuQuantizer();
using(var quantized = quantizer.QuantizeImage(bmp1))
{
quantized.Save(targetPath, ImageFormat.Png);
}
Full explanation of the method is available on this blog post http://www.hurryupandwait.io/blog/convert-32-bit-pngs-to-high-quality-8-bit-pngs-with-c
I also suggest looking at ImageSharp which also works with .NET Core
using (var image = Image.Load(fileData)) // fileData could be file path or byte array etc.
{
var h = image.Size().Height / 2;
var w = image.Size().Width / 2;
var options = new ResizeOptions
{
Mode = ResizeMode.Stretch,
Size = new Size(w, h)
};
image.Mutate(_ => _.Resize(options));
using (var destStream = new MemoryStream())
{
var encoder = new JpegEncoder();
image.Save(destStream, encoder);
// Do something with output stream
}
}
The one major variable in PNG compression is the tradeoff between compression speed and output size. PNG compression can actually be quite slow because it involves searching a data buffer for matching patterns. You can speed up the compression by limiting how much of the buffer the encoder searches.
Your encoder should have a setting that allows you to specify how searching for matches it will do.
IF your input PNG image was not compressed with the encoder searching the entire buffer, you may get some improved compression by searching the full buffer in your application. However, you are unlikely to get a major improvement.
PNG should be losless given pixel size and color depth are consistent.
If looking for something to baseline output size against.
https://pnggauntlet.com/

OutOfMemoryException error in Loop

I am trying to create a Windows app which uploads files to FTP. Essentially, it looks for .jpeg files in a given folder, it reads through the barcodes found in the .jpg files before uploading it into the FTP server, and entering the URL into the database for our records.
As there will be multiple files at any given time in the folder, I am essentially trying to read them in a loop, and process them accordingly. However, I get an OutOfMemoryException whenever the loop starts again. I am trying to figure out what I'm doing wrong here. I have appended my code below:
private void btnProcess_Click(object sender, RoutedEventArgs e)
{
podPath = Directory.GetFiles(DestPath, "*.jpg");
List<string> scans = new List<string>(podPath.Length);
List<string> badscans = new List<string>();
byte[] imageBytes;
string filename, result;
POD conpod = new POD();
OTPOD otpod = new OTPOD();
ConsignmentObj scanJob;
//Pickup OTScan;
//Consolidate ccv;
for (int i = 0; i < podPath.Count(); i++ )
{
filename = podPath[i].ToString();
using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename))
{
var results = barcodeReader.Decode(bm);
result = results.ToString();
bm.Dispose();
}
if (result != null)
{
//if barcode can be read, we throw the value into the database to pull out relevant information
if (result.Contains(ConNotePrefix))
{
#region Consignments
scanJob = getCon(result.ToString());
final = ImageFolder + "\\" + result.ToString() + ".jpg";
using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
{
MemoryStream ms = new MemoryStream();
try
{
img.Save(ms, ImageFormat.Jpeg);
imageBytes = ms.ToArray();
img.Dispose();
}
finally
{
ms.Flush();
ms.Close();
ms.Dispose();
}
}
lock (filename)
{
if (System.IO.File.Exists(filename))
{
File.Delete(filename);
}
}
using (var stream = File.Create(final)) { }
File.WriteAllBytes(final, imageBytes);
File.Delete(filename);
conpod.ConsignmentID = scanJob.ConsignmentID;
conpod.UserID = 1;
conpod.Location = ftpUrl + "//" + result.ToString() + ".jpg";
conpod.rowguid = Guid.NewGuid();
UploadFilesToFtp(ftpUrl, ftpUser, ftpPass, final, result.ToString() + ".jpg");
insertPOD(conpod);
scans.Add(result.ToString());
#endregion
}
}
else
{
badscans.Add(filename);
}
}
this.lbScans.ItemsSource = scans;
this.lbBadScans.ItemsSource = badscans;
}
The FTP method, UploadFilesToFtp(x, x, x, x, x, x) is not a problem here. All feedback will be much appreciated.
An OutOfMemoryException can also be thrown by the method FromFile of the Image class when
The file does not have a valid image format.
or
GDI+ does not support the pixel format of the file.
So i think there is a problem with one of your image files you are reading. One solution is to catch the OutOfMemoryException and adding the file to the badscans.
try{
using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename)) {
var results = barcodeReader.Decode(bm);
result = results.ToString();
bm.Dispose();
}
}
catch(OutOfMemoryException) {
badscans.add(filename);
}

Object currently in use elsewhere (System.Drawing)

This is the code which I run to generate a PDF
public string ScreenshotFullLength(string Url) {
UrlScreenshot Shot = new UrlScreenshot(Url, 975, 100);
int maxHeight = 1250;
// If you do skip the crop function you 'll get the
// full lenght of the page. We'll scale it to match
// a 400 pixel width
//int newHeight = (Shot.Bitmap.Width / 400) * Shot.Bitmap.Height;
//Shot.Resize(400, newHeight);
string Filename = LazyAssFilename();
string path = Server.MapPath("~") + "/tmp/" + Filename;
Shot.Bitmap.Save(path, ImageFormat.Png);
string pdfURL = "";
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
pdfURL = Server.MapPath("~") + "\\tmp\\Attest_" + EOFName + ".pdf";
PdfWriter.GetInstance(document, new FileStream(pdfURL, FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(path);
if (jpg.Height > maxHeight) {
//we moeten er meer dan 1 maken en croppen
int loops = (int)(jpg.Height / maxHeight);
int rest = (int)(jpg.Height % maxHeight);
int i;
for (i = 0; i < loops; i++) {
Bitmap bmpImage = new Bitmap(path);
Bitmap bmpCrop = bmpImage.Clone(new System.Drawing.Rectangle(0, i * maxHeight, 975, maxHeight),
bmpImage.PixelFormat);
iTextSharp.text.Image crpd = iTextSharp.text.Image.GetInstance(bmpCrop, ImageFormat.Png);
crpd.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
crpd.ScalePercent(60);
document.Add(crpd);
}
//the rest
Bitmap bmpImage2 = new Bitmap(path);
Bitmap bmpCrop2 = bmpImage2.Clone(new System.Drawing.Rectangle(0, i * maxHeight, 975, rest),
bmpImage2.PixelFormat);
iTextSharp.text.Image crpdRest = iTextSharp.text.Image.GetInstance(bmpCrop2, ImageFormat.Png);
crpdRest.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
crpdRest.ScalePercent(60);
document.Add(crpdRest);
} else {
jpg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
jpg.ScalePercent(60);
document.Add(jpg);
}
} catch (DocumentException de) {
Console.Error.WriteLine(de.Message);
} catch (IOException ioe) {
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
try {
//screenshots deleten
File.Delete(path);
} catch { }
return pdfURL;
}
This runs on a website, to make a PDF of a webpage.
However when multiple people access this code from the website, to generate their PDF's, I get the error: Object is currently in use elsewhere.
STACKTRACE: at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at ...
How would I fix this? The error gets thrown at Shot.Bitmap.Save(path, ImageFormat.Png);
I encountered the same problem, I have a worker thread pumping out GUI to its graphical interface, but it only throws this exception some of the time.
Wrapping it with an Invoke fixed it.
_parent.Invoke( new Action(() => eg.Graphics.DrawImage( _icon, rc )));
Just spotted this similar question. One of the answers suggests that GDI+ isn't threadsafe, in which case you'll need a lock around your Save() and maybe a few other methods.
Just to confirm, reading the information for the Image class, although it is ambiguous as to whether this means a single instance, or different instances of the same class.
Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.

Categories

Resources