private void StartAsyncTask(object state) { try { // We want to respond to image requests of the form; // // <encoded barcode>.Barcode // Cache information from context _request = _context.Request; _response = _context.Response; // Filename is the encoded design ID BarcodeImageUri uri = new BarcodeImageUri(_request.Url); // Lookup design and retrieve image data // Stream JPEG image to client _response.ContentType = "image/jpeg"; _response.Clear(); _response.BufferOutput = true; // Get the object capable of rendering the barcode BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology( uri.EncodingScheme); // Render barcode and save directly onto response stream using (Image image = drawObject.Draw(uri.Text, uri.BarMinHeight, uri.BarMaxHeight, uri.BarMinWidth, uri.BarMaxWidth)) { image.Save(_response.OutputStream, ImageFormat.Jpeg); } } catch (Exception e) { _error = e; } finally { _response.End(); SetComplete(); } }
private void StartAsyncTask(object state) { try { // We want to respond to image requests of the form; // // <encoded barcode>.Barcode // Cache information from context _request = _context.Request; _response = _context.Response; // Filename is the encoded design ID BarcodeImageUri uri = new BarcodeImageUri(_request.Url); // Lookup design and retrieve image data // Stream JPEG image to client _response.ContentType = "image/jpeg"; _response.Clear(); _response.BufferOutput = true; // Get the object capable of rendering the barcode BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(uri.EncodingScheme); BarcodeMetrics metrics = drawObject.GetDefaultMetrics(30); metrics.Scale = uri.Scale; BarcodeMetrics1d metrics1d = metrics as BarcodeMetrics1d; if (metrics1d != null) { metrics1d.MaxHeight = uri.BarMaxHeight; metrics1d.MinHeight = uri.BarMinHeight; metrics1d.MaxWidth = uri.BarMaxWidth; metrics1d.MinWidth = uri.BarMinWidth; } else if (uri.EncodingScheme == BarcodeSymbology.CodeQr) { BarcodeMetricsQr qrMetrics = (BarcodeMetricsQr)metrics; qrMetrics.EncodeMode = uri.QrEncodingMode; qrMetrics.ErrorCorrection = uri.QrErrorCorrect; qrMetrics.Version = uri.QrVersion; } // Render barcode and save directly onto response stream MemoryStream imageStream = new MemoryStream(); using (Image image = drawObject.Draw(uri.Text, metrics)) { // Save to temporary stream because image tried to seek // during the write operation image.Save(imageStream, ImageFormat.Jpeg); // Move to start of the stream imageStream.Seek(0, SeekOrigin.Begin); // Do synchronous copy to response output stream int blockSize = 1024; byte[] buffer = new byte[blockSize]; while (true) { int bytesRead = imageStream.Read(buffer, 0, blockSize); if (bytesRead == 0) { break; } _response.OutputStream.Write(buffer, 0, bytesRead); } } } catch (Exception e) { _error = e; } finally { _response.End(); SetComplete(); } }