[Closed] Resize non square image using dotNet
How to resize an image that is not square using dotnet?
The code below works perfectly for squared images. The resize fn is written by denisT and can be found on the forum
(
rollout rol_ResizeDNImage ""
(
local ConvertClass = dotnetclass "system.convert"
local ImageClass = dotnetclass "system.drawing.image"
local miauuLogo_70x30 = "iVBORw0KGgoAAAANSUhEUgAAAFgAAABMCAYAAADp0nC5AAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAfaSURBVHhe7dhbjF1VGQfwmel1Ws4w5zIz55y5nHPmXPbea9/PZXqZTgFbKOUSAm0YDIkhYJsg1RhT0FSNGC9BEI0FCxorBqqYEEvgAahVMNEE0hZTaNQqCaFJo28aH/TJ5O+395HYfPke1+ZpNfl10n9Pvp38Z81aa88IACNDYmjoI4aGPmJo6COGhj5iaOgjhoY+YmjoI4aGPmJo6COGhj5iaOgjhoY+YmjoI4aGPmJo6COGhj5iaOgjhoY+YmjoI4aGPmJo6COGhj5iaOgjhhz9Sf4yGN6TRAw5PtgY4j1JxJDjg40h3pNEDDk+OGu3XBvgto8F2L87wr7rI6zeGOLW6yIsRT6WYhe3XOPipp0urt/uYvd2HytbI7TL68VZWeI9ScSQ44Oz5Lem0XS3otLwUGsHqHcClBZ8NO0AX7k/xqf3d1GqxWipCJYbwfZCtL0BAqeF8fXyzKzwniRiyPHBWVkzNorlHStYbFkIA5d48D0PuarCi18MgD90gbe6OHi7h8kq/b/vIfCHX1tqAL++SZybFd6TRAw5Pjgry3GNVmoM33XSchMBlZefdfGjByMquA+c7tK24WGmNvz/D/lhF7bjYConz84C70kihhwfnIWJzevR37IT7U4bUTgsLSIereBm26OtwMPnPxni3pt9VBsuXPq3T+X//7MuOv4AYbsozs8C70kihhwfnIVd2xzU2z6tWJX+2HdDF5bjolJTqDcUZqnUyRmF0ryLhaaLBuXV+vCzyXaSfA3CAG3bR604Jj5DN96TRAw5Pli3+fIkVLQVttVBN/JQb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pba5M35Byhofbh3hPEjHk+GDd6tPr0LQCKOVihl6Ljx4K8P7JLg7f5aEwa6eH1749Hv50IsarXw1pW6FbBK30Fq34lx+O8Bfaq1dvcNNiO7Sn16oF8Tm68Z4kYsjxwVmwWvNUTghXOajWbXSowGRF56ngiRmb9lcbLVrNSbHTC5SVLEzN2XQIOmjS5xYWbfTiEJX5FopXZXewXYn3JBFDjg/OQjE3lr7upm9iDhVs0Y85FXnvjQoPftyj65uNBhWcHHDb6Xr20AEP12xxUKKSLTu5RyssWj5mp7J9ubgS70kihhwfnBW7XkKjE6XXrGTl3k3l4ldd4J0+vkcvGGsKFq1qGxeP0uvxnwf46/GItoukeCo4DLEwv4BN6z+a1ZvgPUnEkOODszJO5SinQ7eJAFfNWDh0J91zz9Ih9scBnvmcT4edRatU4R8nqPT3B/j38136LG0dtHKTX13OlbK9lnG8J4kYcnxwluy5zajbPXr1DVCjg+/Rgz0c/0IvvR1YVHy57uOOPRGee6yP1T0hpmsB4jhGtTKT3kikmVnhPUnEkOODs5T8Hnd2ZhKFQgmlIt0G1uYxMprH5GQBlekiqmTDpuSWkMc6+lotFzGeK2Cu8NH8kv1KvCeJGHJ8sDHEe5KIIccHG0O8J4kYcnywMcR7koghxwcbQ7wniRga+oihoY8YGvqIoaGPGBr6iKGhjxga+oihoY8YGvqIoaGPGBr6iKGhjxga+oihoY8YGvqIoaGPGBr6iKGhjxga+oihoY8YGvqIoaELRv4L8KsVAQB45twAAAAASUVORK5CYII="
dotNetControl dnBtn_01 "System.windows.forms.button" pos:[0,-2] width:70 height:30
function ConvertBase64StringToImage string =
(
bytearr = convertclass.FromBase64String string
memstream = dotnetobject "System.IO.MemoryStream" bytearr
DecodedImg = ImageClass.fromstream memstream
memstream.close()
return DecodedImg
)
function DotnetImageResize image width height =
(
new = dotNetObject "System.Drawing.Bitmap" width height image.PixelFormat
g = (dotnetclass "System.Drawing.Graphics").FromImage new
g.SmoothingMode = g.SmoothingMode.HighQuality
g.CompositingQuality = g.CompositingQuality.HighQuality
g.InterpolationMode = g.InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = g.PixelOffsetMode.highQuality
g.DrawImage image 0 0 width height
new
)
on rol_ResizeDNImage open do
(
img = DotnetImageResize (ConvertBase64StringToImage miauuLogo_70x30) (ceil (70*1.25)) (ceil (30*1.25))
dnBtn_01.image = img
-- non resized image
-- dnBtn_01.image = ConvertBase64StringToImage miauuLogo_70x30
)
)
createdialog rol_ResizeDNImage
)
The image you are resizing isn’t 70×30 pixels as its name suggest, but 88×76. Perhaps that’s the problem?
fn ShowImage img =
(
format "width:% height:%
" img.width img.height
cb = dotnetclass "system.windows.forms.clipboard"
cb.setimage img
display (getclipboardbitmap())
)
You are right, the image is 88x76px, but the dotnet button is with size 70×30 px. When the code is executed in 3dsMax 2016 or earlier there is no problem. But, 3dsMax2017 scales the dotnet button. In my PC the scale factor is 1.25. So the button is scaled to new size 88×38 px, but the image remain with size 70×30 px.
The goal is to resize the image to 88×38 px, but the resize function not works as I want.
Your addition works. The original FN also works.
I found the problem. The miauuLogo_70x30 is with different size 88×76. At the center of the image there is 70x30px area where is the actual image. Surrounding area is blank – the image is PNG so there are trasparent pixels. The Fn resizes the image properly, but it resizes from 88×76 to the new size. The actual image(non transparent pixels) are not resized which is the problem.
Try with this image:
local miauuLogo_70x30 = "iVBORw0KGgoAAAANSUhEUgAAAEYAAAAeCAIAAAAekf9JAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAACHDwAAjA8AAP1SAACBQAAAfXkAAOmLAAA85QAAGcxzPIV3AAAKOWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAEjHnZZ3VFTXFofPvXd6oc0wAlKG3rvAANJ7k15FYZgZYCgDDjM0sSGiAhFFRJoiSFDEgNFQJFZEsRAUVLAHJAgoMRhFVCxvRtaLrqy89/Ly++Osb+2z97n77L3PWhcAkqcvl5cGSwGQyhPwgzyc6RGRUXTsAIABHmCAKQBMVka6X7B7CBDJy82FniFyAl8EAfB6WLw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Qv+l2y0rS21i1pUJRhfRbPJeL3hjgf/OUzbEy6nOtBF7dy8bfQtR2ahEAkUjlve7Pt7hfbz4lFEtDaGp96IoOyfieuXbu6uqEeEfWbWXLNQia6g+IvoebnWEqq7JHJjVZDXAr9Z5+BCPLJDG4Z7W/tNSOTx1BJwOf/x4ujafVKFwpoXHXK6itVgR0NAahpMlGzXezxIq1lY7CiiE9yqrqud0QiUCrNsOQxTBIAazXCn7jl7+6OwM2nI3LgeB/dZ852d5+NRqNdRzo7Dh08eTJ64kT0TyejHZGDJ09F29uSWegGuZMkoP++NHPmvd+X7h8SSPoy85Wk+x8h/gcX4S39buYJoQAAAABJRU5ErkJggg=="
Original FN and your addition works properly, but this time the size of the miauuLogo_70x30 is 70×30 px with no transpernt pixels around.
So the problem was the original image size after all. I am glad you solved it.
You was right in your first post. What is strange to me – when the image is applied to the button(without resizing) the transparent pixels are not shown. Only the area with non transparent pixels are displayed. But, when the same image is resized the transparent pixels are visible. It seems(may be I am wrong) that, before resizing, the image is cropped and the transparent pixels are removed, after the resizing the image is applied to the button in its original view. But the most important is that the problem is solved.