QrCoder Asp.Net - How to remove noise? - asp.net

Right now I'm creating my QR-Code using QrCoder from Asp.Net. You can see my code below:
SvgQrCode:
public void UpdateText(string value)
{
using (var qrGenerator = new QRCodeGenerator())
{
using (var qrCodeData = qrGenerator.CreateQrCode(value, QRCodeGenerator.ECCLevel.Q))
{
using (var qrCode = new QRCode(qrCodeData))
{
using (var bitmap = qrCode.GetGraphic(1, Color.Black, Color.White, false))
{
Image.FromData(bitmap);
}
Text = value;
} } } }
Xml:
<Image Source="{Binding Element.ImageSource}"
RenderOptions.BitmapScalingMode="NearestNeighbor"
RenderOptions.EdgeMode="Aliased"
Width="{Binding Element.Width}"
Height="{Binding Element.Height}"
Stretch="Uniform">
What happens:
The generated QR-Code has some light noise that you can see here in the screenshot on the left side(left side Gray8, right side BlackWhite):
What I tried:
I changed in the method Decode the Pixelformats from Gray8 to BlackWhite. The result is the screenshot above (qr code on the right side).
internal static BitmapSource Decode(string value, int? pixelWidth, BitmapCacheOption cacheOption = BitmapCacheOption.OnLoad)
{
// ..some code..
var grey = new FormatConvertedBitmap(bitmap, PixelFormats.Gray8, BitmapPalettes.Gray256, 1.0);
return grey;
}
Another thing that I tried is changing qrCode.GetGraphic(1) to something higher like qrCode.GetGraphic(10), which increases the pixels per module. But this is not a clean way to fix the problem, because the noise is just getting realy small(so you can hardly see it anymore) and the pixel per module are getting increased.
My Problem: I'm using the method decode for qr codes, bar codes and images. So If I would add an image it would be black and white. Of course I can use an if-statement and check what if its an image or or qr/bar-code. But why is PixelFormats.Gray8 creating noise? Why is it not clean?

I found the problem. I overlooked those lines and didnt realize that .jpeg was choosen as format:
public void FromData(Bitmap bitmap)
{
using (var ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Jpeg);
//...

Related

Replace already drawn bitmap on SK Canvas at particular point

I wanted to update the existing bitmap drawn >#particular coords> on SK Canvas, while calling InvalidateSurface am able to draw but the previous bitmap is not clearing. New Image is beneath the older one! Canvas.Clear(0) didn't clear all previous bitmaps.
Needed help in getting over this issue.
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
canvas.Clear(0) // Dont seems be clearing old Bitmaps
if (condition)
{
using (Stream streami = assembly.GetManifestResourceStream(resourceID))
{
resourceBitmap = SKBitmap.Decode(streami);
}
canvas.DrawBitmap(resourceBitmap, listPoint);
}
else
{
using (Stream streami = assembly.GetManifestResourceStream(secondID))
{
secondBitmap = SKBitmap.Decode(streami);
}
canvas.DrawBitmap(secondBitmap, listPoint); // Drawn beneath the older
}
}
Now the if condition changed to true outside the code blocks with new value again calling canvasView.InvalidateSurface();
Doest not clear previous

Applying a background for the control in an Effect in XamarinForms

I'm trying to apply a background for what ever the control the effect is attached to.
First i made the effect to apply a background for the controls that have either a Fill or Background properties in UWP.
But the problem is a lot of controls renderes to FrameworkElement in UWP. And FrameworkElement doesn't have a Background property .
In the VisualElementRenderer Xamarin forms adresses this issue be applying a background layer behind the content but this is an effect.
Is there is any way to apply a background for whatever the effect is attached to?
I'm appplying an AcrylicBrush So it must be assigned to the Control directly.
The code i written before:
try
{
Control.GetType().GetProperty("Background").SetValue(Control, brush);
}
catch
{
Control.GetType().GetProperty("Fill").SetValue(Control, brush);
}
I'm appplying an AcrylicBrush So it must be assigned to the Control directly.
You could use compositor to apply Acrylic to SpriteVisual. We use Win2D to make GaussianBlurEffect for UIElement before we have Acrylic API. Get Compositor of UIElement, and use this Compositor to CreateSpriteVisual. then set GaussianBlurEffect to hostSprite.Brush like the following.
Compositor _compositor;
SpriteVisual _hostSprite;
private void applyAcrylicAccent(Panel panel)
{
_compositor = ElementCompositionPreview.GetElementVisual(panel).Compositor;
_hostSprite = _compositor.CreateSpriteVisual();
_hostSprite.Size = new Vector2((float)panel.ActualWidth, (float)panel.ActualHeight);
var backdrop = _compositor.CreateHostBackdropBrush();
// Use a Win2D blur affect applied to a CompositionBackdropBrush.
var graphicsEffect = new GaussianBlurEffect
{
Name = "Blur",
BlurAmount = 100f,
Source = new CompositionEffectSourceParameter("backdrop")
};
var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" });
var effectBrush = effectFactory.CreateBrush();
effectBrush.SetSourceParameter("backdrop", backdrop);
_hostSprite.Brush = effectBrush;
ElementCompositionPreview.SetElementChildVisual(panel, _hostSprite);
}
And calling it with applyAcrylicAccent(RootLayout). You also will need to handle the SizeChanged event :
private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (_hostSprite != null)
_hostSprite.Size = e.NewSize.ToVector2();
}

GetImageThumbnail() with dimensions

In our ASP.NET MVC 3 We are currently generating thumbnail images on the fly with the following code:
public void GetImageThumbnail(string imageName)
{
WebImage wbImage = new WebImage("~/assets/images/gallery/"+imageName+".jpg");
int width = 220;
wbImage.Resize(width, (int)((double)wbImage.Height * width / wbImage.Width));
wbImage.FileName = imageName+"_small.jpg";
wbImage.Write();
}
and displaying them in the view like this:
<img src="#Url.Action("GetImageThumbnail", new {imageName = "motherboard"})" alt="" />
How can we control the image size on the view and generate the complete <img> tag from scratch? We need to specify the image dimension and obtain an <img> tag the contains the width and height properties.
Thanks.
My first thought is to create your own HTML helper method which does all of the work. Something you would consume like:
#Html.ThumbnailImg("motherboard")
That method does the thumbnail work and can output the full desired markup. More info: http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs. For what it's worth, I prefer the extension method design. It seems more palatable to other devs.
UPDATE:
There is a little bit of chicken-and-egg that I didn't think about before. Perhaps something like:
public static MvcHtmlString ThumbnailImg(
this HtmlHelper html,
UrlHelper url,
String imageName,
String elementID,
String altText)
{
var img = new TagBuilder("img");
if (String.IsNullOrEmpty(elementID) == false) img.MergeAttribute("id", elementID);
if (String.IsNullOrEmpty(altText) == false) img.MergeAttribute("alt", altText);
// Generate and cache thumnail here, determining height and width
// ...
var src = url.Action("GetImageThumbnail", new { imageName = imageName });
img.MergeAttribute("height", height);
img.MergeAttribute("width", width);
img.MergeAttribute("src", src);
return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
}

Flex 3 - Image cache

I'm doing an Image Cache following this method: http://www.brandondement.com/blog/2009/08/18/creating-an-image-cache-with-actionscript-3/
I copied the two as classes, renaming them CachedImage and CachedImageMap.
The thing is that I don't want to store the image after being loaded a first time, but while the application is being loaded.
For that, I've created a function that is called by the application pre-initialize event. This is how it looks:
private function loadImages():void
{
var im:CachedImage = new CachedImage;
var sources:ArrayCollection = new ArrayCollection;
for each(var cs in divisionData.division.collections.collection.collectionSelection)
{
sources.addItem(cs.toString());
}
for each(var se in divisionData.division.collections.collection.searchEngine)
{
sources.addItem(se.toString());
}
for each( var source:String in sources)
{
im.source = source;
im.load(source);
}
}
The sources are properly retrieved.
However, even if I use the load method, I do not get the "complete" event... As if the image is not being loaded... How is that?
Any help would be appreciated.
Thanks in advance.
Regards,
BS_C3
I found the problem with my code =)
It was a declaration problem.
I moved the declaration of the cachedImage inside the for each loop where the images are loaded. So that I get something like this:
for each( var source:String in sources)
{
var im:CachedImage = new CachedImage;
im.source = source;
im.load(source);
}
And this does the trick.

How to remove or disable hyperlink events from pdf file?

I can display a PDF file from byte[] in asp.net.
The problem is that it contains hyperlinks and I want to disable or remove these hyperlink events.
Docotic.Pdf, the library I am involved with, can be used to find hyperlinks in PDFs and remove them.
Here is the sample code that does exactly this:
public static void RemoveHyperlinks(string inputFile, string outputFile)
{
using (PdfDocument doc = new PdfDocument(inputFile))
{
foreach (PdfPage page in doc.Pages)
{
for (int i = 0; i < page.Widgets.Count; i++)
{
PdfWidget widget = page.Widgets[i];
PdfActionArea actionArea = widget as PdfActionArea;
if (actionArea != null)
{
PdfUriAction linkAction = actionArea.Action as PdfUriAction;
if (linkAction != null)
{
page.Widgets.RemoveAt(i);
i--;
}
}
}
}
doc.Save(outputFile);
System.Diagnostics.Process.Start(outputFile);
}
}
Please note that some viewers can detect hyperlinks from text and still present them as clickable areas even though there is not links defined in PDF itself. For example, Adobe Reader with certain settings can do just that.
P.S. I know this question is old, but maybe my answer will benefit new visitors.
You can libraries like this one to open and modify the PDF file and convert every hyperlink object to simple text.

Resources